/***************************
A SIMPLE PAGINATOR
*************************/
function Paginator(ref, limit) {
this.ref = ref;
this.pageNumber = 0;
this.limit = limit;
this.lastPageNumber = null;
this.currentSet = {};
}
Paginator.prototype = {
nextPage: function (callback) {
if( this.isLastPage() ) {
callback(this.currentSet);
}
else {
var lastKey = getLastKey(this.currentSet);
// if there is no last key, we need to use undefined as priority
var pri = lastKey ? null : undefined;
this.ref.startAt(pri, lastKey)
.limit(this.limit + (lastKey? 1 : 0))
.once('value', this._process.bind(this, {
cb: callback,
dir: 'next',
key: lastKey
}));
}
},
prevPage: function (callback) {
console.log('prevPage', this.isFirstPage(), this.pageNumber);
if( this.isFirstPage() ) {
callback(this.currentSet);
}
else {
var firstKey = getFirstKey(this.currentSet);
// if there is no last key, we need to use undefined as priority
this.ref.endAt(null, firstKey)
.limit(this.limit+1)
.once('value', this._process.bind(this, {
cb: callback,
dir: 'prev',
key: firstKey
}));
}
},
isFirstPage: function () {
return this.pageNumber === 1;
},
isLastPage: function () {
return this.pageNumber === this.lastPageNumber;
},
_process: function (opts, snap) {
var vals = snap.val(), len = size(vals);
console.log('_process', opts, len, this.pageNumber, vals);
if( len < this.limit ) {
// if the next page returned some results, it becomes the last page
// otherwise this one is
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.