Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering backbone views together with collection

I have a collection which contains several items that should be accessible in a list.

So every element in the collection gets it own view element which is then added to the DOM into one container.

My question is: How do I apply the sort order I achieved in the collection with a comparator function to the DOM? The first rendering is easy: you iterate through the collection and create all views which are then appended to the container element in the correct order.

But what if models get changed and are re-ordered by the collection? What if elements are added? I don't want to re-render ALL elements but rather update/move only the necessary DOM nodes.

like image 476
Christian Engel Avatar asked May 17 '12 16:05

Christian Engel


People also ask

What is a backbone collection?

Advertisements. Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

How do I use backbone sync?

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.


1 Answers

model add

The path where elements are added is rather simple, as you get the index in the options when a model gets added to a collection. This index is the sorted index, based on that if you have a straightforward view, it should be easy to insert your view at a certain index.

sort attribute change

This one is a bit tricky, and I don't have an answer handy (and I've struggled with this at times as well) because the collection doesn't automatically reshuffle its order after you change an attribute the model got sorted on when you initially added it.

from the backbone docs:

Collections with comparator functions will not automatically re-sort if you later change model attributes, so you may wish to call sort after changing model attributes that would affect the order.

so if you call sort on a collection it will trigger a reset event which you can hook into to trigger a redraw of the whole list.

It's highly ineffective when dealing with lists that are fairly long and can seriously reduce user experience or even induce hangs

So the few things you get walking away from this is knowing you can:

  • always find the index of a model after sorting by calling collection.indexOf(model)
  • get the index of a model from an add event (3rd argument)

Edit:

After thinking about if for a bit I came up with something like this:

var Model = Backbone.Model.extend({
    initialize: function () {
        this.bind('change:name', this.onChangeName, this);
    },
    onChangeName: function ()
    {
        var index, newIndex;

        index = this.collection.indexOf(this);
        this.collection.sort({silent: true});
        newIndex = this.collection.indexOf(this);
        if (index !== newIndex)
        {
            this.trigger('reindex', newIndex);
            // or
            // this.collection.trigger('reindex', this, newIndex);

        }
    }
});

and then in your view you could listen to

var View = Backbone.View.extend({
    initialize: function () {
        this.model.bind('reindex', this.onReindex, this);
    },
    onReindex: function (newIndex)
    {
        // execute some code that puts the view in the right place ilke
        $("ul li").eq(newIndex).after(this.$el);
    }
});
like image 115
Vincent Briglia Avatar answered Sep 22 '22 11:09

Vincent Briglia