Inheritance 如何在backbone.js中创建具有多种模型类型的集合?
我正试图让它工作,但我挣扎着。当我在Inheritance 如何在backbone.js中创建具有多种模型类型的集合?,inheritance,backbone.js,javascript-framework,Inheritance,Backbone.js,Javascript Framework,我正试图让它工作,但我挣扎着。当我在fetch上检查success回调时,我的集合将变为空。在解析过程中,它没有给我任何明显的错误。这是我的密码: 我的收藏: VOR.Collections.GridItems = Backbone.Collection.extend({ model : VOR.Models.GridItem, url: "assets/data/grid.json", parse: function(response){
fetch
上检查success
回调时,我的集合将变为空。在解析过程中,它没有给我任何明显的错误。这是我的密码:
我的收藏:
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)
});
以下是我创建集合的方式:
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
时,我得到:
对象{length=0,models=[0],_byId={…},更多…}
我还记录了集合中的parse
函数,它可以通过JSON文件正常运行……关于这里可能出现的错误有什么想法吗?msg
对象的长度应为5。这就是parse
函数循环和(应该)向集合添加模型的次数
// **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;
}
});
最好将网格项存储在不同的集合中,并使用如下模型包装它们:
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()
处理这个问题的一个好方法是重新定义model
属性,它告诉集合如何向集合添加新模型,如本文所述:(感谢@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);
}
}
});
谢谢你!这比在parse
中执行要好得多,因为这也适用于引导数据。这正是我想要的。谢谢我认为第一行应该是var VOR.Collections.GridItems=Backbone.Collection.extend({