我的 collection.fetch 不起作用,也无法渲染模型。 我的 router.js 上有这个
itemcollection = new ItemCollection();
itemcollection.fetch();
ItemListView.render();
CartListView.render();
}
我的itemcollection.js
define([
'underscore',
'backbone',
'model/item_model'
],function(_, Backbone, Item){
var ItemCollection = Backbone.Collection.extend({
model: Item,
url: 'http://posbeta.interprisesolutions.com/POSMobileConnector/Product/loaditembycategory/Event Materials',
parse: function(response) {
return response.Items;
}
});
return ItemCollection;
});
我的观点:
initialize: function(){
ItemCollection.bind("reset", this.render );
},
render: function(){
var data = {
items: itemcollection.models
}
var compiledTemplate = _.template( ItemListTemplate , data);
$("#itemContainer").html( compiledTemplate );
},
有趣的是,当我使用 firebug 调试它时,它会正确呈现,当我删除断点时,它不会显示任何内容。有什么想法吗?
最佳答案
请记住,fetch
是异步的 - 它会在数据加载之前立即返回。
你可以尝试这样的事情:
itemcollection = new ItemCollection();
itemcollection.fetch({
success: function () {
ItemListView.render();
CartListView.render();
}
});
或者,您可以监听集合的 reset
事件。
关于javascript - 获取集合上没有模型。 Backbone 中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972313/