Search code examples
backbone.jspagination

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?


Solution

  • 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/