Skip to content

Instantly share code, notes, and snippets.

@krosenberg
Created September 4, 2014 18:05
Show Gist options
  • Save krosenberg/771a4e31a56f0c25d9cb to your computer and use it in GitHub Desktop.
Save krosenberg/771a4e31a56f0c25d9cb to your computer and use it in GitHub Desktop.
A ShowMoreCollection for client-side-paginated Backbone.PageableCollection
/*
showMore() Adds more models to the first page of a pageable collection.
It does this by resetting the collection to the current collection plus a number of models
from fullCollection, and increases the page size to the number of currently-visible models.
*/
Backbone.ShowMoreCollection = Backbone.PageableCollection.extend({
showMore: function(num) {
var num = num || 10;
var state = this.state;
var pageNum = state.currentPage;
var pageSize = state.pageSize + num;
this.state = this._checkState(_.extend({}, state, {currentPage: pageNum, pageSize:pageSize}));
var last = this.fullCollection.indexOf(this.last());
var nextSlice = this.fullCollection.slice(last, last+num);
var currentModels = this.models;
var comparator = this.comparator;
this.comparator = null;
this.reset(_.union(currentModels, nextSlice))
this.comparator = comparator;
if (comparator) this.sort();
// return true if there are more to show
return this.length < this.fullCollection.length;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment