Sorting a Backbone Collection After initialization

There’s a discussion on this very topic that you might want to look at: https://github.com/documentcloud/backbone/issues/41.

The short of it is that when a user selects ‘sort by X’, you can:

  1. Set the comparator function on the Collection
  2. Call the Collection’s sort function (which will trigger a sort event)
  3. Listen for the sort event in your View, and (clear and) redraw the items

Another way to handle steps 1 & 2 is to have your own method that calls the Collection’s sortBy method and then triggers a custom event that your View can listen to.

But it seems to be the case that clearing and redrawing is the easiest (and maybe even the fastest) way to sort your View’s and keep them in sync with your Collection’s sort order.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)