HeyJude:
Pushing the button in the following code does 2 things: It executes a Function in a nested view model, and also makes a computed
in the parent view model to execute.
var MasterViewModel = function () {
var self = this;
self.nested = new FirstViewModel();
self.func = ko.computed (function() {
var items = self.nested.array();
alert("executed");
});
}
var FirstViewModel = function () {
var self = this;
self.array = ko.observableArray([]);
self.push = function () {
self.array.push(new SecondViewModel());
alert("pushed");
}
}
var SecondViewModel = function () {
var self = this;
self.z = ko.observable();
}
var mvm = new MasterViewModel();
ko.applyBindings(mvm);
However, if the computed
is changed to a simple function, it doesn't execute when the button is pushed. Why?
var MasterViewModel = function () {
var self = this;
self.nested = new FirstViewModel();
self.func = function() {
var items = self.nested.array();
alert("executed");
};
}
var FirstViewModel = function () {
var self = this;
self.array = ko.observableArray([]);
self.push = function () {
self.array.push(new SecondViewModel());
alert("pushed");
}
}
var SecondViewModel = function () {
var self = this;
self.z = ko.observable();
}
var mvm = new MasterViewModel();
ko.applyBindings(mvm);
Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE