主干未使用为集合指定的模型。我肯定错过了什么。Backbone.js未使用集合模型值
App.Models.Folder = Backbone.Model.extend({
initialize: function() {
_.extend(this, Backbone.Events);
this.url = "/folders";
this.items = new App.Collections.FolderItems();
this.items.url = '/folders/' + this.id + '/items';
},
get_item: function(id) {
return this.items.get(id);
}
});
App.Collections.FolderItems = Backbone.Collection.extend({
model: App.Models.FolderItem
});
App.Models.FolderItem = Backbone.Model.extend({
initialize: function() {
console.log("FOLDER ITEM INIT");
}
});
var folder = new App.Models.Folder({id:id})
folder.fetch();
// later on change event in a view
folder.items.fetch();
该文件夹被加载,然后加载项目,但它们不是FolderItem对象,并且从不调用FOLDER ITEM INIT。它们是基本的模型对象。
我错过了什么?我应该这样做吗?
编辑: 不知道为什么这项工作与文档,但下面的作品。主干5.3
App.Collections.FolderItems = Backbone.Collection.extend({
model: function(attributes) {
return new App.Models.FolderItem(attributes);
}
});
啊,这是有道理的。谢谢! – Candland
这让我感到很开心。它造成了非常神秘的错误。我认为这应该在文档中突出显示。 – UpTheCreek