Get previous value of an observable in subscribe of same observable

前端 未结 5 1579
抹茶落季
抹茶落季 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:03

    I have found that I can call peek() from a writable computed observable to get the before value.

    Something like this (see http://jsfiddle.net/4MUWp):

    var enclosedObservable = ko.observable();
    this.myObservable = ko.computed({
        read: enclosedObservable,
        write: function (newValue) {
            var oldValue = enclosedObservable.peek();
            alert(oldValue);
            enclosedObservable(newValue);
        }
    });
    

提交回复
热议问题