Create a paginated version of a backbone collection that stays in sync.
Create a read-only paginated version of a backbone collection that stays in sync.
var superset = /* ... */;// By default there are 20 models per page, but you can configure thisvar paginated = superset perPage: 100 ;// Assuming superset.length === 401assertpaginatedgetNumPages === 4;assertpaginatedgetPage === 0;assertpaginatedlength === 100;assertpaginatedhasNextPage;assert!paginatedhasPrevPage;// Go to the next pagepaginatednextPage;assertpaginatedgetPage === 1;// Move to the last pagepaginatedsetPage3;assertpaginatedlength === 1;
Initialize a new PaginatedCollection by passing in the original collection and optionally
an options hash with the number of models per page. If no
perPage argument is passed
the collection will always maintain the length of the original collection.
var paginated = originalCollection;// orvar paginated = originalCollection perPage: 15 ;
Change the number of models displayed per page. This will reset the current page to 0.
Change the page. If the page is less than 0, it will be set to 0. If it is longer than the number of pages, the last page will be selected.
Return the current setting for number of models per page.
Return the current number of pages.
Return the current page. E.G. if this returns 0, you're on the first page.
Returns true if this is not the last page.
Returns true if this is not the first page.
delta pages forwards or backwards (if
delta is negative).
paginated.movePage(-2) will move two pages back.
Move to the next page. Equivalent to
Move to the previous page. Equivalent to
Move to the first page of the collection. Equivalent to
Move to the last page of the collection. Equivalent to
paginated.setPage(paginated.getNumPages() - 1).
Get rid of any paginated settings. This means the paginated collection will always be equal to the superset.
Return a reference to the original collection.
Remove all ties to the superset and stop updating. Will now be garbage collected when it falls out of scope.
reset should fire as you expect.
paginated:change:perPage - Fired whenever the number of models per page is changed. If you
remove the pagination settings,
perPage will be passed as
paginated:change:page - Fired whenever the page is changed.
paginated:change:numPages - Fired whenever the number of pages is changed.
paginated:destroy - Fired when the proxy is destroyed
Install with Bower:
bower install backbone-paginated-collection
The component can be used as a Common JS module, an AMD module, or a global.
Install with npm, use with Browserify
> npm install backbone-paginated-collection
and in your code
var PaginatedCollection = require'backbone-paginated-collection';
You can include
backbone-paginated-collection.js directly in a script tag. Make
sure that it is loaded after underscore and backbone. It's exported as
on the global object.
<script src="underscore.js"></script><script src="backbone.js"></script><script src="backbone-paginated-collection.js"></script>
Install Node (comes with npm) and Bower.
From the repo root, install the project's development dependencies:
npm installbower install
Testing relies on the Karma test-runner. If you'd like to use Karma to automatically watch and re-run the test file during development, it's easiest to globally install Karma and run it from the CLI.
npm install -g karmakarma start
To run the tests in Firefox, just once, as CI would: