0
我試圖用集合上最後十個模型或更少的模型做一個列表。我有事件來動態添加創建模型。這個事件在集合上調用'add',並用我的邏輯添加正確的一個元素。但我需要添加新的元素,檢查是否收藏有超過10,如果這是真的刪除最後一個模型,並添加新的骨幹集合總是與n模型
var model = Backbone.Model.extend({
defaults: function() {
return {id:null}
}
});
var collection = Backbone.Collection.extend({
model:model
});
var view = Backbone.View.extend({
initialize: function(){
var self = this;
this.listenTo(collection, 'add', this.addOne);
this.listenTo(collection, 'reset', this.addAll);
this.listenTo(collection, 'all', this.render);
},
render: function(){
this.$el.html();
return this;
},
addAll: function(){
this.collection.each(this.addOne, this);
},
addOne: function(model){
//this is executed after 'create' but before this I need slice my collection
var view = new view({model:model});
this.$el.prepend(view.render().el);
}
});
感謝