Oguz Karadenizli
Oguz Karadenizli

Reputation: 3505

Prevent running of computed properties (dependentobservable) in knockout.js

I want to prevent syncer property while constructing, because I could send persistedObject to X class.

    function X(persistanceObject) {
        var self = this;

        self.xid = -1;
        self.syncer = ko.computed(function () {
            // if  object construction in progress then return 

            // persist properties....
        }, self);
        self.y = ko.observable(43);

        if (typeof persistanceObject !== 'undefined') {
            ko.mapping.fromJS(persistanceObject, {}, self.y);
        }
    }

Upvotes: 0

Views: 147

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

You can prevent a computed observable from evaluating immediately by using the deferEvalaution option.

It would look like:

self.syncer = ko.computed(function () {
            // if  object construction in progress then return 

            // persist properties....
}, self, { deferEvaluation: true });

Now it will not evaluate until its value is accessed. If you are not binding this in your UI, then you would want to call it at least once (self.syncer()) after all of your properties are ready.

Upvotes: 2

Related Questions