Change observable but don't notify subscribers in knockout.js

后端 未结 4 1240
情歌与酒
情歌与酒 2020-11-28 05:33

Is there a way to ignore subscribers on a value change of the observable. Id like to change a value of an observable, but not execute it for the subscribers with knockout.j

4条回答
  •  旧时难觅i
    2020-11-28 06:10

    An even simpler approach:

    ko.observable.fn.silentUpdate = function(value) {
        this.notifySubscribers = function() {};
        this(value);
        this.notifySubscribers = function() {
            ko.subscribable.fn.notifySubscribers.apply(this, arguments);
        };
    };
    

    Use it as follows:

    this.status = ko.observable("Happily Married");
    this.walkIntoBar();
    this.status.silentUpdate("Single");
    this.walkOutOfBar(); // careful with exceptions
    this.status.silentUpdate("Happily Married");
    

    To be used with caution. We are dealing with an observable object so bad things can happen if you fail to notify your subscribers.

提交回复
热议问题