Blog: 2015 April

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...