Comments (8)
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.
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.
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.
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.
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.
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.
You're welcome!
from knockout.mapping.
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)
- mapping fails if value = "constructor" HOT 2
- Is there a way to update the nested properties of the items of an ObservableArray? HOT 1
- Method visitPropertiesOrArrayEntries is doing unnecessary iterations on updateViewModel because the wrong variable is being used to check against ignored properties.
- Mapping not saved when data is an array HOT 1
- Mapping doesn't work when properties listed in different order HOT 4
- Empty observe array in mapping options creates observables from all properties HOT 1
- ko.mapping.toJSON to implement JSON formatting param like ko.toJSON
- Calling fromJS to amend an existing model with an ObservableArray fails
- NuGet package install directory
- Questions about Knockout Mapping HOT 10
- The "create" handler does not work with non-array properties HOT 1
- Create handler causes mapping to evaluate pureComputed HOT 1
- Suggestion: Safely update mapping when property in new object happens to be null
- mapping partial model into existing viewModel
- [Question] is arrayChanged part of the public api?
- objectLookup() + "watch" + Firefox = hilarity
- bug of optimizedKeys
- optionsCaption causes bound "value" to be deleted
- Doesn't seem to work with KO 3.4.2 HOT 1
- Having redudant data passed to an object makes it so the second reference of the same object is not updated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from knockout.mapping.