Backbone没有使用为集合指定的模型.我肯定错过了什么.
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();
Run Code Online (Sandbox Code Playgroud)
加载文件夹,然后加载项目,但它们不是FolderItem对象,永远不会调用FOLDER ITEM INIT.它们是基本的Model对象.
我错过了什么?我应该这样做吗?
编辑: 不知道为什么这对文档有效,但以下工作.骨干5.3
App.Collections.FolderItems = Backbone.Collection.extend({
model: function(attributes) {
return new App.Models.FolderItem(attributes);
}
});
Run Code Online (Sandbox Code Playgroud) backbone.js ×1