backbone.js 模型获取

发布于 2025-01-05 05:23:18 字数 684 浏览 0 评论 0原文

模型的奇怪行为。在点击事件中,我试图获取模型(它已经填充了集合,但我需要在点击时重新获取它)。一切顺利,它接收数据,但模型本身不会改变,而是为属性设置新数据,它添加新属性 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2025-01-12 05:23:18

也许问题是您返回单个对象的 json 数组

[{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}]

而不是

{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}

[] 应该出局。请检查它是否会像这样工作。

Maybe the problem is that you are returning json array for single object

[{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}]

instead of

{"body":"..","id":"4f24e353e599194e07000003","level":"ERROR","tag":"DED00","time":"2012-01-29 10:12:35","trace":"..."}

[] should be out. Please check if it will work like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文