Proper way to sort a backbone.js collection on the fly

前端 未结 6 1155
孤独总比滥情好
孤独总比滥情好 2021-01-30 02:31

I can successfully do this:

App.SomeCollection = Backbone.Collection.extend({
  comparator: function( collection ){
    return( collection.get( \'lastName\' ) );         


        
6条回答
  •  难免孤独
    2021-01-30 03:01

    Change to comparator function by assigning a new function to it and call sort.

    // Following example above do in the view:
    
    // Assign new comparator
    this.collection.comparator = function( model ) {
      return model.get( 'lastname' );
    }
    
    // Resort collection
    this.collection.sort();
    
    // Sort differently
    this.collection.comparator = function( model ) {
      return model.get( 'age' );
    }
    this.collection.sort();
    

提交回复
热议问题