我正在尝试列出最后十个或更少模型的列表。我有使用 create 动态添加模型的事件。此事件在集合上调用“添加”,并根据我的逻辑正确添加一个元素。但我需要添加新元素,检查集合是否超过 10,如果是真的删除最后一个模型并添加新的
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);
}
});
谢谢