如何在不重新渲染的情况下使用Layoutmanager对嵌套视图进行重新排序/排序?
问题描述:
我正在使用Backbone.Layoutmanager.js进行Backbone项目如何在不重新渲染的情况下使用Layoutmanager对嵌套视图进行重新排序/排序?
我得到了一个带有嵌套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();
}
答
您不需要手动调用排序方法。学习一下吧: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);
}
希望这有助于