javascript Knockout Force 在设置新值时通知订阅者一个 observable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29194528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:13:15  来源:igfitidea点击:

Knockout Force notify subscribers of an observable when setting new value

javascriptknockout.js

提问by Alexander Abakumov

Suppose we want to assign a new value to an observable and notify subscribers regardless of whether the new value equals or not to the old value.

假设我们想为一个 observable 分配一个新值并通知订阅者,无论新值是否等于旧值。

By default, Knockout won't notify subscribers if the new value is the same as old one, so we need to take some extra steps to achieve our goal.

默认情况下,如果新值与旧值相同,Knockout 不会通知订阅者,因此我们需要采取一些额外的步骤来实现我们的目标。

I know There is extender currentPage.extend({ notify: 'always' })but I need that behavior in a specific place only, not globally for an observable.

我知道有扩展器,currentPage.extend({ notify: 'always' })但我只需要在特定位置的行为,而不是全局的可观察的。

Currently, I'm using the following approach:

目前,我正在使用以下方法:

    // Some view model property of primitive type
    self.currentPage = ko.observable(1);

    // Some view model method
    self.foo = function (newPage) {
        var currentPageObservable = self.currentPage;

        // Save the old value
        var oldCurrentPageValue = currentPageObservable();

        // Update the observable with a new value
        currentPageObservable(newPage);

        if(oldCurrentPageValue === newPage) {
            // If old and new values are the same - notify subscribers manually
            currentPageObservable.valueHasMutated();
        }
    };

But that looks like it could be better.

但这看起来可能会更好。

Why Knockout just doesn't provide, for instance, a method for assigning a new value to an observable that always notifies subscribers?Or am I missing such one?
And what are your approaches for achieving the same task?

例如,为什么 Knockout 不提供一种为始终通知订阅者的 observable 分配新值的方法?还是我错过了这样一个?
您完成相同任务的方法是什么?

回答by Max Brodin

Your approach is good enough, except you may want to refactor it in order not to notify subscribers twice, when the value has changed.

您的方法已经足够好了,除非您可能想要重构它,以免在值更改时两次通知订阅者。

if (oldCurrentPageValue !== newPage) {
   // Update the observable with a new value
   currentPageObservable(newPage);
}
else {
   // If old and new values are the same - notify subscribers manually
   currentPageObservable.valueHasMutated();       
}

In your case currentPageObservable(newPage)notifies subscribers and right after that valueHasMutatedwill notify subscribers second time.

在您的情况下currentPageObservable(newPage)通知订阅者,之后valueHasMutated将第二次通知订阅者。

Another approach would be extend ko.observablewith specific methods

另一种方法是ko.observable使用特定方法进行扩展

ko.myObservable = function Observable(initialValue) {
   var result = ko.observable(initialValue);
   result.updateWithNotification = function (newValue) {
      ...
   }
   return result;
}

var o = ko.myObservable();
o.updateWithNotification(newValue);