knockout.js - Set initial value of computed or supress the first evaluation -


[this seems impossible. answer question myself later]

this question largely continues my pervious one. use "async computed" approach refresh parts of page. michael best solved issue updates of invisible parts of ui. 1 annoying thing still here. how can set initial (default) value computed observable? try avoid multiple ajax calls during page load. instead of embed json page load @ once. seems trivial (common)? can't supress first evaluation of async computed. ajax call made in case. use approach:

var isfirsteval = ko.observable(true); updatecomputed = ko.purecomputed(function () {     updatetrigger();     if(isfirsteval()){         isfirsteval(false);         result(initialvalue);     }     else         result(evaluator.call(owner)); }); 

but face the same issue, in previous question: computed never subscribe on evaluator changes, because of approach knockout uses reevaluate computed observables. suggestion similar question works because checks first evaluation after var value = self.product() * self.quantity();. computed tracking product , quantity. can't use approach, because can't call evaluator care dependency evaluator can make ajax call...

does way exists supress first evaluation of computed (or pure computed)? or maybe way set initial value? suggestions? other workarounds?

you can use deferevaluation option:

    var c1 = ko.computed({         read: function() {             // computations             return "some value";         },         deferevaluation: true     }); 

you can use computed context determine initial computation (the same document).

update

you can use "isinitial" determine whether computed being evaluated first time:

var mycomputed = ko.computed(function() {     var isfirstevaluation = ko.computedcontext.isinitial();     if(isfirstevaluation) {         // code     }     else {         // code     } }); 

Comments

Popular posts from this blog

javascript - Hide toolbar of pdf file opened inside iframe using firefox -

Ansible - ERROR! the field 'hosts' is required but was not set -

Copy range with conditional formatting -