Fiddler here: http://jsfiddle.net/u9PLF/
I got a list of parents with nested children. I periodically update the whole object hierarchy and update the first parent's children array once. The problem I'm having is that it seems like the children array is always being updated, or at least it notifies the subscribers every time I call the fromJS function.
I expect the children observableArray to only notify the subscribers once (when it first change from [A,B] to [A,B,C], not on subsequent calls).
What am I doing wrong?
Thanks
Code:
var data = {
parents: [{
id: 1,
name: 'Scot',
children: ['A', 'B']
}]
};
function ParentVM(data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.count = ko.observable(0);
self.hello = ko.computed(function () {
console.log("Update", self.children());
self.count(self.count() + 1);
});
}
var mapping = {
parents: {
create: function (options) {
return new ParentVM(options.data);
},
key: function (data) {
return ko.utils.unwrapObservable(data.id);
}
}
};
var viewModel = ko.mapping.fromJS(data, mapping);
ko.applyBindings(viewModel);
setInterval(function () {
var data = {
parents: [{
id: 1,
name: 'Scott',
children: ['A', 'B', 'C']
}]
};
ko.mapping.fromJS(data, mapping, viewModel);
}, 2000);
viewModel.parents.subscribe(function () {
console.log("Parents notif");
});