Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in Backbone.js

I know that there is a component for this but based on what i see you have to create a new collection with the component extended. Is there another way to do pagination in backbone?

All i need is just a previous and next button limit the items per page to 12. i've been creating it on javascript ( not a good solution for production environment ). Any ideas?

like image 487
n0minal Avatar asked May 06 '12 12:05

n0minal


1 Answers

Since Backbone collection has underscore methods extended, you might want to create helper pagination method very easy. I use something like :

var Paginated = Backbone.Collection.extend({

    pagination : function(perPage, page) {
       page = page-1;
       var collection = this;
       collection = _(collection.rest(perPage*page));
       collection = _(collection.first(perPage));    
       return collection.map( function(model) { return model.toJSON() } ); 
    }
});

This returns toJSON of your collection, you may play with it in the jsfiddle : http://jsfiddle.net/YHmrp/2/

like image 92
drinchev Avatar answered Sep 18 '22 02:09

drinchev