I’m trying to use the example mentioned for using the previous api in Backbone’s Model. I’ve pasted the 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 alert is Bill Smith which is correct. But if I try to access this in firebug by calling
bill.previous("name");
it is Bill Jones and not Bill Smith. Why is it so? What am I missing ?
Well, if you look at Backbone’s source, you’ll see that the
_previousAttributesattribute inBackbone.Modelis reset to a copy of the current attributes after the ‘change’ events have been fired (in thechangemethod:this._previousAttributes = _.clone(this.attributes);).I think I’ve seen issues on Github about this behavior before; it’s unexpected to a lot of people.
Edit: found it; a quote from the author in https://github.com/documentcloud/backbone/pull/373 :