2012-02-24 8 views
19

バックボーンコンパレータが実装されていますが、基本的にルートに基づいて異なるソート方法を選択し、コンパレータを使用してコレクションをソートする必要があります。理想的には、コレクション内にカプセル化されたソートロジックを保持したいが、固まっているようだ。たとえば、が正しくバックボーンコンパレータを実装しています

Requests = Backbone.Collection.extend({ 
     model : Request, 
     comparator : function(ab) { 
      return -ab.id; 
     },   
     nooffers : function() { 
      return this.sortBy(function(ab) {    
       return ab.get('offers'); 
      }); 
     } 
    }); 

デフォルトでは、デフォルトのコンパレータに基づいてソートされますが、私のルーティングでは、

routes : { 
     "" : "index", 
     '/ordering/:order' : 'ordering' 
    }, 
    ordering : function(theorder) { 
     ordering = theorder; 
     if(theorder == 'nooffers') { 
      Request.comparator = Request.nooffers(); 
     } 
     Request.sort(); 
     listView.render(); 
     howitworksView.render(); 
    } 

この場合、エラー(「c.callは機能ではありません」)が表示されます。

+1

回答は良かったですが、質問にも答えが出てきました。:-) – jmk2142

答えて

47

ここではいくつかの問題があります。

これは、あなたがそれがないと思う何をしません:nooffersメソッドを実行し、Request.comparatorにその結果を割り当て

if(theorder == 'nooffers') { 
    Request.comparator = Request.nooffers(); 
} 

を。

nooffers : function() { 
    return this.sortBy(function(ab) {    
     return ab.get('offers'); 
    }); 
} 

とコンパレータ機能は何も有効ではありませんように、そのリストを設定する:しかし、ソートされたリストをsortBy返します。

あなたは、その戻り値ということではなく機能を使用するために割り当てを変更したい:

if(theorder == 'nooffers') { 
    Request.comparator = Request.nooffers; 
} 

、有効なコンパレータ機能する機能を変更します。

nooffers : function(ab) { 
    return ab.get('offers'); 
} 

デモ(あなたと実行コンソールが開いている):http://jsfiddle.net/ambiguous/AAZCa/

しかし、外部からの誰かがそのコレクションのメソッドを操作していると悪いことになりますそれをやる。代わりに、あなたはこのようなもので、その順序を変更するには、コレクションを依頼する必要があります。

var Requests = Backbone.Collection.extend({ 
    model: Request, 
    comparator: function(ab) { 
     if(this._order_by == 'offers') 
      return ab.get('offers'); 
     else if(this._order_by == 'id') 
      return -ab.id; 
     //... 
    }, 
    order_by_offers: function() { 
     this._order_by = 'offers'; 
     this.sort(); 
    }, 
    order_by_default: function() { 
     this._order_by = 'id'; 
     this.sort(); 
    }, 
    _order_by: 'id' 
}); 
//... 
rs.order_by_offers(); 

はデモ:http://jsfiddle.net/ambiguous/uM9av/

それとも、コレクションはcomparator内のすべての条件ロジックを避けるためにcomparator独自のを交換しましょうことができます:

var Requests = Backbone.Collection.extend({ 
    model: Request, 
    initialize: function() { 
     this._order_by_id = this.comparator; 
    }, 
    comparator: function(ab) { 
     return -ab.id; 
    }, 
    order_by_offers: function() { 
     this.comparator = this._order_by_offers; 
     this.sort(); 
    }, 
    order_by_default: function() { 
     this.comparator = this._order_by_id; 
     this.sort(); 
    }, 
    _order_by_offers: function(ab) { 
     return ab.get('offers'); 
    } 
}); 

デモ:私はカスタムメートルを書いた

+2

これは努力だけでは正しくない場合でも+1します; – ggozad

+0

@ggozad:ありがとう。私はバグを避ける方法と、現在のバグを修正する方法を人々に教えるのが好きです。そして、http://jsfiddle.net/は多くの努力を払っています。 –

+0

本当にありがとう、本当にこのすべてを説明する時間を感謝します! – Xrender

0

http://jsfiddle.net/ambiguous/Pjfq2/両方の昇順の並べ替えと降順の世話をしますし、また、それはまた、英数字適切

var LetterVariables = Backbone.Collection.extend({ 



    initialize: function (id) { 

     //id of the table 
     this.id = id; 

     this.sortVar = "id"; //default sorting column 
     this.sOrder = "asc" //default sort order 
    }, 
    //comparator function that will create a descending and an ascending order tot he view 
    comparator: function (item,itemb) { 
     var a=item.get(this.sortVar); 
     var b=itemb.get(this.sortVar); 
     if (this.sOrder == "asc") { 
      return this.SortCustom(a, b); 
     } 
     return -this.SortCustom(a, b); 
    }, 
    SortCustom:function(a,b){ 
       if (isNaN(a)) { 
        if (isNaN(b)) { 
         if (a > b) return 1; // before 
         if (b > a) return -1; // after 
         return 0; 
        } 
        return 1; 
       } 
       if (isNaN(b)) { 
        return -1; 
       } 
       if (+a > +b) return 1; // before 
       if (+b > +a) return -1; // after 
       return 0; 
    }, 
    Sort: function (by, order) { 

     this.sOrder = order; 
     this.sortVar = by; 
     this.sort(); 
    }}); 

//でレコードをソートしますが、「並び替え」の方法を使用して並べ替えることができ、コレクション内のethod(Sortメソッドのために密接に大文字のSを見て)

関連する問題