Strangely I noticed that the model previous method is not working the way I thought.. it keeps returning the same value as the get. I think that something is wrong with my code or backbone.js is not upgrading the this._previousAttributes when change event is fired.
model = new Backbone.Model()
model.set({attr1: 123})
alert(model.previous("attr1")) //alert 123 instead of undefined
alert(model.get("attr1"))
model.set({attr1: 312})
alert(model.previous("attr1")) //alert 321 instead of 123
alert(model.get("attr1"))
What am I doing wrong?
The
previousmethod is only useful while a"change"event is happening:The
previousmethod is only useful inside a"change"event handler; similar things apply tohasChanged,changedAttributes, andpreviousAttributes.You’re trying to use
previouswhen you’re not inside an event handler so you get nonsense. If you want to know what has changed in a model and you need to know outside of"change"event handlers, then you’ll have to track it yourself.