2014-02-24 5 views
-1

컬렉션에 따라 페이지에 페이지 매김을 구현하고 싶습니다. 각 페이지는 20 개 항목을 보유해야하므로 페이지 1은 첫 번째 20 개 모델, 두 번째 페이지 두 번째 20 개 항목 등을 반복해야합니다.백본 컬렉션 반복하기

여기에 몇 가지 질문을 읽은 후에이를 수행 할 수 있음을 발견했습니다. 다음 방법으로 :

_.each(this.collection.slice(startIndex,startIndex+itemsPerPage),function(item) { 
     console.log(item.get("name")); 
    }); 

더 좋은 방법이 있습니까?

답변

0

나는 this one을 사용하고 그것은 아주 잘 작동합니다

Backbone.Collection.prototype.partialEach = function partialEach(offset, maxItemsPerPage, iterator, context) { 
    for (var l = this.length; maxItemsPerPage !== 0 && offset < l; offset++) { 
     var model = this.at(offset); 
     if(model) { 
      iterator.call(context, model, offset, this); 
      maxItemsPerPage--; 
     } 
    } 
};