knockout.js - Knockout js computed observable executed every time it is referenced -
knockout.js - Knockout js computed observable executed every time it is referenced -
i have view model in have observable array. have computed observable calculated 1 time observable array populated looping through items of array , computing computed observable.
this computed observable referenced in other computed observables. each time computed observable referenced looping done 1 time again. can whatever 1 suggest how can avoid this. computed observable executes looping once.
depending on how have declared computed observable (i'm assuming typical ko.computed(evaluator)
, every time read
computed evaluator
execute evaluate it.
considering have stated "this computed observable referenced in other computed observables", every 1 of references performing read
.
in general, need "cache" result of processing.
the easiest way prevent duplicate processing not utilize computed straight in dependency chain. instead, within evaluator you'd modify other observable property of viewmodel , found dependencies on observable.
var vm = {data: ko.observablearray(), obs: ko.observable()} vm.modifiesmodelproperty = ko.computed(function(){ vm.obs("some evaluated look " + vm.data()) }) vm.anothercomputed = ko.computed(function(){ homecoming vm.obs(); })
if insistent on using computed , wanted encapsulate caching within it.
vm.computed = ko.computed({ owner: (function(){ var self = this; self.cachingobservable = ko.observable() vm.data.subscribe(function(nv){ self.cachingobservable("some evaluated look " + nv) }); })(), read: this.cachingobservable, })
you utilize combination of both approaches , set owner
viewmodel , modify observable property way. in case, read homecoming observable.
knockout.js
Comments
Post a Comment