Modifying existing Knockout.js computed observable -
i have scenario nice able change read method of knockout computed observable in order change how calculates result. tried straight re-creating computed, unfortunately made field stop computing. i'm assuming wholly replacing computed observable broke behind scenes in dependency logic. suggestions?
there no way change "read" function of computed observable. can have conditionals within function , call function:
self.xyz = ko.purecomputed(function () { return self.somecondition() ? self.methoda() : self.methodb(); }); you have computed call method within view-model, can redefine @ time. if want make sure computed updated whenever update function, should use observable store function:
self.methoda = ko.observable(function () { /*initial calculation*/ }); self.xyz = ko.purecomputed(function () { return self.methoda()(); }); // sometime later self.methoda(function () { /*new calculation*/ });
Comments
Post a Comment