javascript - Accessing computed values between view models -


i trying include multiple viewmodels on 1 page, don't want tie these specific page ids know you're able want able use each model potentially more once on page , use with binding.

i'm trying read value of self.total in vm1, self.total in vm2, i'm getting error:

cannot write value ko.computed unless specify 'write' option. if wish read current value, don't pass parameters.

reading standard vm1.x variables vm2 seem work fine, knockout complains when trying computed value.

javascript:

var vm1 = function(parent) {   var self = this;    self.costa = 1000;   self.costb = 500;    self.total = ko.computed(function(){     return self.costa + self.costb;   }); };  var vm2 = function(parent) {   var self = this;    self.total = ko.observable(parent.vm1.total); };  var masterviewmodel = function() {   var self = this;    self.vm1 = new vm1(self);   self.vm2 = new vm2(self); };  window.masterviewmodel = new masterviewmodel();  ko.applybindings(window.masterviewmodel); 

html:

<div data-bind="with: vm1">   <p data-bind="text: total()"></p> </div> <div data-bind="with: vm2">   <p data-bind="text: total()"></p> </div> 

the solution (as noted @haim770) call total function: self.total = ko.observable(parent.vm1.total());


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -