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

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

问题描述

有没有办法忽略观察者的价值变化的订阅者。 id喜欢改变一个可观察的值,但不能为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.

这是一个扩展名功能到可观察:

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;
};

您可以将其添加到可观察的像:

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天全站免登陆