迭代骨干集合
我已经为 Users
设置了一个主干集合,当我执行 fetch 方法时,我会返回一个 JSON 对象,格式如下:{"users": [{...}, {...}, ...],大小:数字}
来自服务器。令人困惑的是,当我执行下面的代码时,我得到的不是每个 user
对象,而是一个“子”对象,它有两个属性:users 和大小;谁能帮我理解为什么?谢谢。
display: function(){
this.collection.each(function(user){
console.log("main", user);
});
}
I've setup a backbone collection for Users
and when I execute the fetch method, I get back a JSON object along the lines of: {"users": [{...}, {...}, ...], size: number}
from the server. Confusingly, when I execute the code below, instead of getting each user
object, I get a single "child" object, which has two attributes: users and size; can anyone help me understand why? Thanks.
display: function(){
this.collection.each(function(user){
console.log("main", user);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在集合上添加一个名为 parse 的方法:
Add a method on the collection called parse:
这对我来说非常有意义。查看 JSON:它有两个属性:users 和 size。
您可能只想迭代
collection.users
:或者,只是分配
collection
到foo.users
而不是foo
(其中foo
是通过解析返回的 JSON 创建的对象)。This makes perfect sense to me. Look at the JSON: it has two properties: users and size.
You probably just want to iterate over
collection.users
:Alternately, just assigned
collection
tofoo.users
instead offoo
(wherefoo
is the object created by parsing the returned JSON).