Skip to content
This repository has been archived by the owner on May 17, 2018. It is now read-only.

Commit

Permalink
Fix #41 and #52 server can now return partial state and 0 as total_en…
Browse files Browse the repository at this point in the history
…tries
  • Loading branch information
wyuenho committed Mar 12, 2013
1 parent a68de1f commit 4645069
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
10 changes: 6 additions & 4 deletions lib/backbone-pageable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
var _result = _.result;
var _bind = _.bind;
var ceil = Math.ceil;
var max = Math.max;

var BBColProto = Backbone.Collection.prototype;

Expand Down Expand Up @@ -562,15 +563,15 @@
throw new RangeError("`firstPage must be 0 or 1`");
}

state.lastPage = firstPage === 0 ? totalPages - 1 : totalPages;
state.lastPage = firstPage === 0 ? max(0, totalPages - 1) : totalPages;

if (mode == "infinite") {
if (!links[currentPage + '']) {
throw new RangeError("No link found for page " + currentPage);
}
}
else {
if (firstPage === 0 && (currentPage < firstPage || currentPage >= totalPages)) {
if (firstPage === 0 && (currentPage < firstPage || (currentPage >= totalPages && totalPages > 0))) {
throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got " + currentPage + '.');
}
else if (firstPage === 1 && (currentPage < firstPage || currentPage > totalPages)) {
Expand Down Expand Up @@ -693,7 +694,7 @@
var links = this.links = {};
var firstPage = state.firstPage;
var totalPages = ceil(state.totalRecords / state.pageSize);
var lastPage = firstPage === 0 ? totalPages - 1 : totalPages || firstPage;
var lastPage = firstPage === 0 ? max(0, totalPages - 1) : totalPages || firstPage;
for (var i = state.firstPage; i <= lastPage; i++) {
links[i] = this.url;
}
Expand Down Expand Up @@ -1028,7 +1029,8 @@

_each(_pairs(_omit(queryParams, "directions")), function (kvp) {
var k = kvp[0], v = kvp[1];
newState[k] = serverState[v];
var serverVal = serverState[v];
if (!_isUndefined(serverVal) && !_.isNull(serverVal)) newState[k] = serverState[v];
});

if (serverState.order) {
Expand Down
Loading

0 comments on commit 4645069

Please sign in to comment.