Get previous value of an observable in subscribe of same observable

前端 未结 5 1582
抹茶落季
抹茶落季 2020-12-07 15:22

Is it possible in knockout to get the current value of an observable within a subscription to that observable, before it receives the new value?

Example:

<         


        
5条回答
  •  被撕碎了的回忆
    2020-12-07 16:07

    ko.subscribable.fn.subscribeChanged = function (callback) {
        var oldValue;
        this.subscribe(function (_oldValue) {
            oldValue = _oldValue;
        }, this, 'beforeChange');
    
        this.subscribe(function (newValue) {
            callback(newValue, oldValue);
        });
    };
    

    Use the above like this:

    MyViewModel.MyObservableProperty.subscribeChanged(function (newValue, oldValue) {
    
    });
    

提交回复
热议问题