Reputation: 1191
I know about 'Drilling down into arrays/objects' from the documentation ( http://knockoutjs.com/documentation/plugins-mapping.html ), but I want this to work for every object in an array of objects, not only for one index like in the example
var data = {
a: "a",
b: [{ b1: "v1" }, { b2: "v2" }]
};
var result = ko.mapping.fromJS(data, { observe: "b[0].b1"});
Is there a way to do this or do I have to run through the array and do it manually anyway? (then I wouldn't need the mapping plugin to begin with).
Upvotes: 1
Views: 215
Reputation: 139798
You cannot do this with the "drilling down" syntax but you can nest the mapping configurations using the create
option:
var result = ko.mapping.fromJS(data, {
observe: " ", // to copy every other property, a in this example
b: {
create: function (options) {
return ko.mapping.fromJS(options.data, {
observe: "b1"
});
}
}
});
Demo JSFiddle.
Using this only the b1
properties will be observable:
Upvotes: 4