2012-02-24 3 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(); 

데모 : 당신이 comparator 자신의 컬렉션 스왑을 할 수 http://jsfiddle.net/ambiguous/uM9av/

또는이 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'); 
    } 
}); 

데모 : 나는 정의 m를 썼다

+2

비록 이것이 혼자 노력하는 것이 옳지 않더라도 +1 할 수 있습니다.) – ggozad

+0

@ggozad : Thanks . 저는 사람들에게 현재 버그를 수정하는 것뿐만 아니라 버그를 피하는 방법을 가르치기를 좋아합니다. 그리고 http://jsfiddle.net/ 많은 노력이 걸립니다. –

+0

정말 고마워,이 모든 것을 설명하기 위해 시간을 내 주셔서 감사합니다! – Xrender

0

http://jsfiddle.net/ambiguous/Pjfq2/ 모두 오름차순 정렬 및 내림차순 돌봐 또한 그것은 또한 숫자와 기록을 정렬합니다 컬렉션의 ethod 적절

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(); 
    }}); 

는 // 당신은 (분류 방법에 대해 자세히 대문자 S를 보는 "정렬"방법을 사용하여 정렬 할 수 있습니다)

관련 문제