When 'create' option is used in knockout mapping we would make the array an observable array. But how do we make the properties of each object in the observable array an observable ?
In this example from the knockout documentation, the children array is made an observable array but I want to make all the elements like id, name within each object literal also to be an observable. How do we achieve that. Just put a ko.observable
on each newed object in the create block ?
var data = {
name: 'Graham',
children: [
{ id : 1, name : 'Lisa' }
]
};
// Your custom data model
var myChildModel = function (data) {
this.id = data.id;
this.name = data.name;
};
var mapping = {
'children': {
create: function(options) {
return new myChildModel(options.data);
}
}
};
var viewModel = ko.mapping.fromJS(data, mapping);