基本上,我正在尝试向我的节点服务器发送一个 GET 请求,以便我可以取回博客文章以创建链接。我做了一个collection.fetch
,它成功完成了 GET 请求(节点服务器记录它正在发送正确的对象)。该模型成功解析了正确的数据,但是当我尝试使用该集合时,它说它是空的。这是代码:
var mdm = mdm || {};
// MODEL
mdm.Post = Backbone.Model.extend({
parse: function( response ) {
response.id = response._id;
console.log(response); // logs the two documents
return response;
}
});
// COLLECTION
mdm.Posts = Backbone.Collection.extend({
model: mdm.Post,
url: '/api/posts'
});
// MODEL VIEW
mdm.LinkView = Backbone.View.extend({
template: _.template( $('#link_template').html() ),
render: function() {
this.$el.html( this.template( this.model.toJSON() ));
return this;
}
});
// COLLECTION VIEW
mdm.LinksView = Backbone.View.extend({
el: '#link_list',
initialize: function() {
this.collection = new mdm.Posts();
this.collection.fetch({reset: true});
// makes the request properly, but collection is empty
this.render();
// never gets called because the collection is empty
console.log(this.collection.length);
// logs a length of 0
},
render: function() {
// renders collection
}
});
$(function() {
new mdm.LinksView();
});
数据正在发送并在模型中解析,所以我不确定集合最终是空的。任何帮助将不胜感激。