When the Create option is used in the knockout view, we will make the array an observable array. But how can we make the properties of each object in an observable array observable?
In this example, from the knockout documentation, the children array is created by the observable array, but I want all the elements, such as id, name in each object literal, to also be visible. How do we achieve this? Just put ko.observable on every new 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);
source share