如何使用布局管理器重新排序/排序嵌套视图而不重新渲染

How to re-sort / order nested Views with Layoutmanager without re-rendering?

本文关键字:排序 新渲染 嵌套 视图 布局 何使用 管理器 新排序      更新时间:2023-09-26

我正在使用Backbone.Layoutmanager进行Backbone项目.js

我得到了一个带有嵌套 ReceiverView 的列表视图。

我的集合是无序更新的 - 我想对这些视图进行排序,但我不想重新呈现整个集合。(因为我在旧视图中丢失了旧数据/事件处理程序/图形实例。

如何解决?

  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);
}

希望这有帮助