我正在使用Backbone.Layoutmanager.js进行Backbone项目
我有一个带有嵌套ReceiverViews的ListView。
我的集合无序更新 - 我想对这些视图进行排序但我不想重新渲染整个集合。 (因为我在旧视图中放松了旧的数据/事件处理程序/图形实例。)
如何解决?
ReceiverListView = Backbone.View.extend({
manage:true,
initialize: function(options){
_.bindAll(this, "renderReceiver","renderMe");
this.vent = _.extend({}, Backbone.Events);
this.collection.on('add', this.renderMe, this);
},
renderMe: function(model1){
this.collection.sort(this.collection.comparator);
this.insertView(new ReceiverView({model: model1})).render();
}
答案 0 :(得分:0)
您不需要手动调用sort方法。了解它:http://backbonejs.org/#Collection-sort
initialize: function () {
this.listenTo(this.collection, 'sort', _.bind(this.onSortCollection, this));
},
onSortCollection: function (collection) {
var views = {};
_.each(this.getViews(), function (view) {
if (view.model) views[view.model.cid] = view;
});
collection.each(function (model) {
var view = views[model.cid];
if (view) this.el.appendChild(view.el);
}, this);
}
希望这有帮助