Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Why does a function in a child view model causes a function in the parent view model to fire?

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


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Why does a function in a child view model causes a function in the parent view model to fire?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×