javascript - Backbone.js Model previous method not working? - Stack Overflow

Strangely I noticed that the model previous method is not working the way I thought.. it keeps returnin

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?

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"))

http://jsfiddle/wLKBk/

What am I doing wrong?

Share Improve this question asked May 19, 2012 at 6:21 mateusmasomateusmaso 8,5036 gold badges42 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The previous method is only useful while a "change" event is happening:

previous model.previous(attribute)

During a "change" event, this method can be used to get the previous value of a changed attribute.

The previous method is only useful inside a "change" event handler; similar things apply to hasChanged, changedAttributes, and previousAttributes.

You're trying to use previous when 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745484114a4629689.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信