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

前端 未结 6 1014
孤独总比滥情好
孤独总比滥情好 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 02:59

    So, this was my solution that actually worked.

      App.Collection = Backbone.Collection.extend({
    
        model:App.Model,
    
        initialize: function(){
          this.sortVar = 'firstName';
        },
    
        comparator: function( collection ){
          var that = this;
          return( collection.get( that.sortVar ) );
        }
    
      });
    

    Then in the view, I have to M-I-C-K-E-Y M-O-U-S-E it like this:

    this.collections.sortVar = 'lastVar'
    
    this.collections.sort( this.comparator ).each( function(){
      // All the stuff I want to do with the sorted collection... 
    });
    

    Since Josh Earl was the only one to even attempt a solution and he did lead me in the right direction, I accept his answer. Thanks Josh :)

提交回复
热议问题