The previous backbone.js attribute is not persistent

I am trying to use the example mentioned for using the previous api in the Backbone Model. I pasted an example below

var bill = new Backbone.Model({
  name: "Bill Smith"
});

bill.bind("change:name", function(model, name) {
  alert("Changed name from " + bill.previous("name") + " to " + name);
});

bill.set({name : "Bill Jones"});

The previous value in the warning is Bill Smith, which is correct. But if I try to access this in firebug by calling

bill.previous("name");

this is bill jones, not bill smith. Why is this so? What am I missing?

+5
source share
1 answer

Well, if you look at the source of Backbone, you will see that the attribute _previousAttributesin Backbone.Modelis reset for a copy of the current attributes after the change events (in change:) were fired this._previousAttributes = _.clone(this.attributes);.

, Github ; .

: ; https://github.com/documentcloud/backbone/pull/373:

, - , . "". hasChanged() .

, .

+13

All Articles