backbone.js 模型获取
模型的奇怪行为。在点击事件中,我试图获取模型(它已经填充了集合,但我需要在点击时重新获取它)。一切顺利,它接收数据,但模型本身不会改变,而是为属性设置新数据,它添加新属性 0 作为对象并将所有新数据存储在那里。
这是它收到的 json:
[{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}]
当我执行 model.toJSON() 时,它返回
0 -> object (body = "new data", id, level, tag, time)
body = 'old data',
...
集合已填充此 json:
[{"body":"...","id":"4f21ed41e599194107000000","level":"ERROR","time":"2012-01-27 04:18:09"},{"body":"...","id":"4f233613e599194607000001","level":"ERROR","time":"2012-01-28 03:41:07"}]
所以我尝试向模型添加新属性,但它有默认值..
Strange behaviour of model. On a click event, i'm trying to fetch the model ( it has been populated with collection already, but I need to refetch it on click). Everything goes fine, it receives the data, but model itself doesn't change rather to set new data for attributes it adds new attribute 0 as object and stores there all new data.
this is json it receives:
[{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}]
and when I do model.toJSON() it returns
0 -> object (body = "new data", id, level, tag, time)
body = 'old data',
...
collection has been populated with this json:
[{"body":"...","id":"4f21ed41e599194107000000","level":"ERROR","time":"2012-01-27 04:18:09"},{"body":"...","id":"4f233613e599194607000001","level":"ERROR","time":"2012-01-28 03:41:07"}]
so i'm trying to add new attributes to the model, it has defaults though..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许问题是您返回单个对象的 json 数组
而不是
[] 应该出局。请检查它是否会像这样工作。
Maybe the problem is that you are returning json array for single object
instead of
[] should be out. Please check if it will work like that.