2011-08-22 72 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

相关问题