this.model.toJSON() 没有返回集合所在的项目,为什么?
我有:
NotificationModel = App.BB.Model.extend({
defaults : {
read : false
},
urlRoot : '/notifications'
});
NotificationCollection = App.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
this.fetch();
},
});
NotificationView = App.BB.View.extend({
tagName : 'li',
render : function() {
console.log((this.model.toJSON())
}
});
此日志不返回从服务器获取的项目。但是当我执行 me.collection.length 时,我确实得到了正确的长度。我做错了什么?
谢谢
I have:
NotificationModel = App.BB.Model.extend({
defaults : {
read : false
},
urlRoot : '/notifications'
});
NotificationCollection = App.BB.Collection.extend({
model: NotificationModel,
url: '/notifications',
initialize : function() {
this.fetch();
},
});
NotificationView = App.BB.View.extend({
tagName : 'li',
render : function() {
console.log((this.model.toJSON())
}
});
This log is not returning the items fetched from the server. But when I do me.collection.length I do get the correct length. What am I doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其他人发表的评论提到了 fetch 方法是异步的。所以
this.fetch()
立即返回。如果您在 this.fetch 之后立即渲染视图,则集合仍将为空。您应该在呈现集合的视图中放置类似这样的内容:
The comments others have made are making reference to the
fetch
method being asynchronous. Sothis.fetch()
returns immediately. If you are rendering your view immediately after this.fetch, the collection will still be empty.You should put something like this in the view that renders your collection: