0

我正在使用 Backbone.Layoutmanager.js 开发一个主干项目

我有一个带有嵌套 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();
 }
4

1 回答 1

0

您不需要手动调用排序方法。了解它: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);
}

希望这可以帮助

于 2014-11-21T02:11:24.040 回答