在knockout.js 中更改可观察但不通知订阅者 [英] Change observable but don't notify subscribers in knockout.js

查看:26
本文介绍了在knockout.js 中更改可观察但不通知订阅者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 observable 的值变化时忽略订阅者.我想改变一个 observable 的值,但不为使用 Knockout.js 的订阅者执行它

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

推荐答案

通常这是不可能或不可取的,因为它可能会导致依赖链中的事物不同步.使用油门扩展器通常是限制依赖项接收通知数量的好方法.

Normally this is not possible or advisable, as it potentially allows things to get out of sync in the dependency chains. Using the throttle extender is generally a good way to limit the amount of notifications that dependencies are receiving.

然而,如果你真的想这样做,那么一种选择是覆盖可观察对象上的 notifySubscribers 函数并让它检查一个标志.

However, if you really want to do this, then one option would be to overwrite the notifySubscribers function on an observable and have it check a flag.

这是一个将这个功能添加到 observable 的扩展:

Here is an extensions that adds this functionality to an observable:

ko.observable.fn.withPausing = function() {
    this.notifySubscribers = function() {
       if (!this.pauseNotifications) {
          ko.subscribable.fn.notifySubscribers.apply(this, arguments);
       }
    };

    this.sneakyUpdate = function(newValue) {
        this.pauseNotifications = true;
        this(newValue);
        this.pauseNotifications = false;
    };

    return this;
};

您可以将其添加到一个 observable 中,例如:

You would add this to an observable like:

this.name = ko.observable("Bob").withPausing();

然后你会在没有通知的情况下更新它:

Then you would update it without notifications by doing:

this.name.sneakyUpdate("Ted");

这篇关于在knockout.js 中更改可观察但不通知订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆