Skip to content

Instantly share code, notes, and snippets.

@krosenberg
Created December 2, 2015 17:16
Show Gist options
  • Save krosenberg/7810a6ee96418f695807 to your computer and use it in GitHub Desktop.
Save krosenberg/7810a6ee96418f695807 to your computer and use it in GitHub Desktop.
A very rough concept for a collection that fetches pages of data with varying-length lists .
var RandomSize = Backbone.Collection.extend({
pageSize: 25,
nextPage: 1,
fullCollection: undefined,
url: '/forensics/api/random/',
parse: function(resp) {
return resp.data
},
getNextPage: function() {
var self = this;
var currentLength = this.fullCollection.length;
if (currentLength > (this.pageSize*(this.nextPage-1) + this.pageSize)) {
_fillNextPage();
} else {
this.fullCollection.fetch({
kache:false,
remove:false,
url: this.url,
success: function() {
if ((self.fullCollection.length - currentLength) < 25) {
self.getNextPage();
} else {
_fillNextPage();
}
}
});
}
function _fillNextPage() {
var start = self.pageSize*(self.nextPage-1);
self.add(self.fullCollection.slice(start, start+self.pageSize));
self.nextPage += 1;
}
return this;
}
});
var rs = new RandomSize();
rs.fullCollection = new RandomSize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment