Search code examples
knockout.jsknockout-2.0observable

Get previous value of an observable in subscribe of same observable


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:

this.myObservable = ko.observable();
this.myObservable.subscribe(function(newValue){
    //I'd like to get the previous value of 'myObservable' here before it's set to newValue
});

Solution

  • There is a way to do a subscription to the before value like this:

    this.myObservable = ko.observable();
    this.myObservable.subscribe(function(previousValue){
        //I'd like to get the previous value of 'myObservable' here before it's set to newValue
    }, this, "beforeChange");