Is it possible to dynamically determine the comparator function for a collection?

I have a basic collection:

myCollection = Backbone.Collection.extend({
  model: myModel
  url: '/theurl',
  initialize: function(){
    this.fetch();
  })
})

Upon initialization, the collection receives items ordered by date. I would like to be able to dynamically change the collection order using a different model attribute (name, rating, etc.).

In the collection-related view, I tried to associate the click event with a callback as follows:

reorderByName: function(){
  myCollection.comparator = function(item){
    return item.get('name');
  });
  this.render();
})

Unfortunately this does not work. Any suggestions on how I can do this?

Thank.

+3
source share
2 answers

, , . , . - :

myCollection.sort();

, reset, , . , :

myCollection.sort({silent: true});

, .

+6

, . , , - , , undefined , , . , , .;)

0

All Articles