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

110 views Asked by At

I am working on a Backbone Project with Backbone.Layoutmanager.js

Ive got a ListView with nested ReceiverViews.

My collection is updated unordered - i want to sort these views BUT i dont want to re-render the whole collection. ( because i loose old data / event handler / graph instance inside old views. )

How to fix ?

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

There are 1 answers

0
knpsck On

You don't need to call sort method manually. Learn about it: 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);
}

Hope this helps