2011-08-22 87 views
7

我試圖讓這個工作,但我與它鬥爭。當我檢查回調fetch時,我的收藏最終爲空。在parse期間,它不會給我任何明確的錯誤。這裏是我的代碼:如何用backbone.js中的幾種模型創建一個集合?

我的收藏:

VOR.Collections.GridItems = Backbone.Collection.extend({ 
     model : VOR.Models.GridItem, 
     url: "assets/data/grid.json", 
     parse: function(response){ 
      var self = this; 

      _.each(response, function(griditem){ 
       switch(griditem.type){ 
        case "news": 
         self.add(new VOR.Models.NewsGridItem(griditem)); 
         break; 
        default: 
         self.add(new VOR.Models.StandardGridItem(griditem)); 
         break; 
       } 
      }); 
     } 
}); 

這是我如何創建集合:

griditems = new VOR.Collections.GridItems(); 

griditems.fetch({ 
    error: function(e) {console.log(e);}, 
    success: function(msg) { 
     console.log(msg) 
    }); 

當我控制檯登錄msg我得到: 對象{長度= 0,模型= [0],_byId = {...},更多...}

我還記錄集合中的parse函數,它通過JSON文件運行得很好......任何這裏有什麼可能是錯誤的想法? msg對象的長度應爲5..i.e。這是parse函數循環多少次並(應該)將模型添加到集合中。

回答

0
// **parse** converts a response into a list of models to be added to the 
// collection. The default implementation is just to pass it through. 
parse : function(resp) { 
    return resp; 
}, 

這是文檔說你應該在解析中做的事情。無論你返回什麼,都將被設置爲收集起始數組。這是它被稱爲來自:

options.success = function(resp) { 
    collection[options.add ? 'add' : 'refresh'](collection.parse(resp), options); 
    if (success) success(collection, resp); 
    } 

所以我建議你解析更改爲:

return _.map(response, function(griditem){ 
    switch(griditem.type){ 
     case "news": 
      return new VOR.Models.NewsGridItem(griditem); 
      break; 
     default: 
      return new VOR.Models.StandardGridItem(griditem); 
      break; 
    } 
}); 
1

這將是更好的網格項目存儲在不同的集合,並與像模型包裝他們這樣的:

var model = Backbone.Model.extend({ 
    url: 'assets/data/grid.json' 
    newsItems: Backbone.Collection.extend({ 
     model: VOR.Models.NewsGridItem 
    }), 
    standartItems: Backbone.Collection.extend({ 
     model: VOR.Models.StandardGridItem 
    }), 

    initialize: function() { 
     this.newsItems = new this.newsItems(); 
     this.standartItems = new this.standartItems(); 

     this.newsItems.bind('all', function() { 
      this.trigger.apply(this, arguments); 
     }, this) 
     this.standartItems.bind('all', function() { 
      this.trigger.apply(this, arguments); 
     }, this) 
    }, 

    parse: function(request) { 
     _.each(response, _.bind(function(griditem) { 
      switch (griditem.type) { 
       case "news": 
        this.newsItems.add(griditem); 
        break; 
       default: 
        this.standartItems.add(griditem); 
        break; 
      } 
     }, this)); 
    } 
}) 

model.fetch() 
18

處理這方面的一個很好的辦法就是重新定義了model屬性,它告訴收集如何將新模型添加到收藏,因爲在這篇文章中解釋:A Backbone.js Collection of multiple Model subclasses(感謝@rulfzid,誰回答我的問題:))

在你的情況,你應該能夠定義模型屬性,像這樣:

var VOR.Collections.GridItems = Backbone.Collection.extend({ 

    url: "assets/data/grid.json", 

    model: function(attrs, options) { 
    switch(attrs.type) { 
     case "news": 
     return new VOR.Models.NewsGridItem(attrs, options); 
     default: 
     return new VOR.Models.StandardGridItem(attrs, options); 
    } 
    } 

}); 
+0

感謝這個!這比'parse'中的更好,因爲這也適用於引導數據。 – philoye

+0

這正是我所期待的。謝謝! –

+0

我認爲第一行應該是'var VOR.Collections.GridItems = Backbone.Collection.extend({ – marcos82

相關問題