Blog: KnockoutJS

Manually triggered computed observables in KnockoutJS

April 3, 2015, (updated on April 21, 2015), Software Development

In KnockoutJS I sometimes need to create a computed observable which does not automatically detect the dependencies and which has to be triggered manually. This may be required if the computation is very complex and should only run in certain situations. To implement this special type of computable, extend the ko object with a new manualComputed method: ko.manualComputed = function (func) { var changes = 0; var dummy = ko.observable(); function observable() { dummy(); var temp = ko.computed(func); var result = temp.peek(); temp.dispose(); return result; } ko.subscribable.call(observable); observable.update = function () { dummy(++changes); }; ko.utils.extend(observable, ko.observable["fn"]); return observable; }; After […] Read more...

Tags: ,


  Page 1 of 1