admin管理员组文章数量:1426839
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 badges1 Answer
Reset to default 6The 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.
本文标签: javascriptBackbonejs Model previous method not workingStack Overflow
版权声明:本文标题:javascript - Backbone.js Model previous method not working? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745484113a2660305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论