-
Notifications
You must be signed in to change notification settings - Fork 28
Pagination
Slickback provides a constructor for paginated collections, Slickback.PaginatedCollection, which can be used in place of Backbone.Collection, e.g.:
var productsCollection = Slickback.PaginatedCollection.extend({
model: Product,
url: '/products'
});
A PaginatedCollection emulates a Slick.Data.DataView (from slick.model.js), by implementing setPagingOptions, getPagingInfo, and the SlickGrid Events handlers for onPagingInfo, onRowCountChanged and onRowsChanged, and can be passed to the constructors for Slick.Grid and Slick.Controls.Pager:
var products = new productsCollection();
var gridOptions = {
formatterFactory: Slickback.BackboneModelFormatterFactory
};
var grid = new Slick.Grid('#grid',products,productColumns,gridOptions);
var pager = new Slick.Controls.Pager(products,grid,$('#pager'));
The pager subscribes to onPagingInfoChanged notifications (see Events). The PaginatedCollection publishes these notifications when it resets its pagination parameters, such as after a fetch.
Slickback uses a naming convention for pagination parameters which is derived from that used by Ruby's popular WillPaginate gem.
Page number and size are passed to the collection's fetch method as page and per_page when the fetchWithPagination method is invoked. Backbone's sync implementation conveys these parameters to jQuery/Zepto ajax.
In order to include pagination parameters, the response should be an object rather than an array. The following parameters are parsed:
- entries (the list of models)
- currentPage
- perPage
- totalEntries
Sample Ruby on Rails code for patching WillPaginate::Collection to serialize as a compatible object (instead of a json array) is provided in the examples directory. This code can be installed as a Rails initializer.