2012-04-02 96 views
0

我collection.fetch不起作用,并没有得到要呈现的模型。 我有这个在我的router.js没有模型在提取collection.fetch骨干

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它调试它正确呈现当我删除断点时,它不显示任何东西。有任何想法吗?

回答

3

熊编码认为fetch是异步 - 它返回立即在数据加载之前。

你可以尝试这样的:

itemcollection = new ItemCollection(); 
itemcollection.fetch({ 
    success: function() { 
    ItemListView.render(); 
    CartListView.render(); 
    } 
}); 

或者,你可以对集合的reset事件听。

+0

我有重置,但它不工作。 ItemCollection.bind(“reset”,this.render); – jongbanaag 2012-04-02 07:33:16

+0

您可能需要绑定'this':'ItemCollection.on('reset',this.render,this);' – stusmith 2012-04-02 07:43:30

+0

我想要求澄清。如果我已经在itemlist视图上实例化了ItemCollection,我是否需要再次实例化它,如果我想在另一个视图上使用它呢? – jongbanaag 2012-04-02 07:54:39

0

该网址中有一个空间:

... /事件材料,

尝试删除,或考虑与%20

+0

hmm。谢谢,但它之前工作。因为在我返回实例化集合之前。 “返回新的ItemCollection”,因为有人说它是一个不好的做法,所以我删除它。 – jongbanaag 2012-04-02 07:28:26