Git Product home page Git Product logo

Comments (8)

sagacity avatar sagacity commented on August 17, 2024

This is due to the way the plugin stores which items have already been mapped and which have not been mapped (inside the __ko_mapping__ property in mappedObject).

The idea is that if you want to add new items you would do this through an update with fromJS or by using mappedCreate.

from knockout.mapping.

wjbryant avatar wjbryant commented on August 17, 2024

I see. So is it possible to change the order of an observable array created with fromJS? Should I not be using the splice, sort, push, etc. methods when using the mapping plugin?

I tried mappedObject.items.mappedCreate({name: 'item 4'}); to add an item, but it exhibited the same issue. mappedObject.items.push(ko.mapping.fromJS({name: 'item 4'})); seems to work, but I'm not sure if that's the right way to do it.

Thanks for your help,
Bill

from knockout.mapping.

sagacity avatar sagacity commented on August 17, 2024

Your 'workaround' using fromJS is completely valid and that's pretty much the intended use. However, when you splice in a copy of another mapped object then the system gets confused. In your workaround however you are creating a new mapped object and inserting that. That should work fine.

The mappedCreate is a convenience function that only works when you have a create callback, otherwise it inserts your unmapped object in the array. That's odd behavior and I'm thinking about removing this functionality entirely.

Does that make things a bit clearer?

from knockout.mapping.

wjbryant avatar wjbryant commented on August 17, 2024

Thanks. That's very helpful. My only other question then is - is there a safe way to move an item from one position to another without confusing the system? That's originally what I was trying to accomplish using the knockout-sortable plugin.

from knockout.mapping.

sagacity avatar sagacity commented on August 17, 2024

To be honest I'm also currently using the sortable plugin without any of these issues, which makes it doubly odd.

However, the difference is that I'm using the create callback to create the separate items in my array, like so:

var mapping = {
  items: {
    create: function(data) {
      return new MyItem(data.data);
    }
  }
};

Maybe you could give that approach a shot, just as a test?

from knockout.mapping.

wjbryant avatar wjbryant commented on August 17, 2024

Thanks for the tip! That works perfectly! I modified my original example using the sortable plugin to use the create callback and that small change fixed the problem.

Original version (does not work): http://jsfiddle.net/WDrep/2/
Modified version (with create callback): http://jsfiddle.net/WDrep/11/

I would have never thought that would make a difference. Thanks for your help!

from knockout.mapping.

sagacity avatar sagacity commented on August 17, 2024

You're welcome!

from knockout.mapping.

vantreeseba avatar vantreeseba commented on August 17, 2024

I didn't see this issue when I commented on #65.

On the mappedCreate callback, and perhaps the push itself, we could wrap the value inserted into the array by default with a call to ko.mapping.fromJS(). This isn't super elegant, but it would fix any confusion with adding objects to mapped arrays.

It would cause any object added to a mapped array to behave "as expected" in that the full object added would always get returned, instead of returning an empty object.

from knockout.mapping.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.