0
votes

I have an input text like below:

<input type="text" style="width: 80%" data-bind="value: Note, event: { blur: $root.UpdateNote}" />

It contains the value of observable variable Note. And on blur it updating note. My problem is to check the previous value of note with current value of note before updating.

Here is my constructor containing all variables

function Goal(data) {
var self = this;

self.GoalID = data.GoalID;
self.Note = ko.observable(data.Note);

}

In my viewmodel i have declared note as observable variable like below:

self.Note = ko.observable();

In knockoutjs, how to get previous value of an observable variable??

1

1 Answers

0
votes

You can subscribe to Note observable changes and specify "beforeChange" parameter to get old value:

function Goal(data) {
    var self = this;

    self.GoalID = data.GoalID;
    self.Note = ko.observable(data.Note);

    self.Note.subscribe(function(oldValue) {
        console.log(oldValue);
    }, self, "beforeChange");

}

Here is an example: http://jsfiddle.net/vyshniakov/HACbc/