Created
December 2, 2015 17:16
-
-
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 .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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