diff --git a/api/index.html b/api/index.html
index bc6ecb8..3e76932 100644
--- a/api/index.html
+++ b/api/index.html
@@ -43,7 +43,7 @@
Backbone
-
+
diff --git a/api/output/Backbone.PageableCollection.js b/api/output/Backbone.PageableCollection.js
index f47c364..2188346 100644
--- a/api/output/Backbone.PageableCollection.js
+++ b/api/output/Backbone.PageableCollection.js
@@ -255,7 +255,7 @@ Ext.data.JsonP.Backbone_PageableCollection({
]
},
- "linenr": 124,
+ "linenr": 123,
"files": [
{
"filename": "backbone-pageable.js",
diff --git a/lib/backbone-pageable.js b/lib/backbone-pageable.js
index bb70650..b79494d 100644
--- a/lib/backbone-pageable.js
+++ b/lib/backbone-pageable.js
@@ -70,7 +70,6 @@
var BBColProto = Backbone.Collection.prototype;
function finiteInt (val, name) {
- val *= 1;
if (!_.isNumber(val) || _.isNaN(val) || !_.isFinite(val) || ~~val !== val) {
throw new TypeError("`" + name + "` must be a finite integer");
}
@@ -574,7 +573,7 @@
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)) {
+ else if (firstPage === 1 && (currentPage < firstPage || currentPage > totalPages && totalPages > 0)) {
throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got " + currentPage + '.');
}
}
diff --git a/lib/backbone-pageable.min.js b/lib/backbone-pageable.min.js
index e05c6db..3b379b2 100644
--- a/lib/backbone-pageable.min.js
+++ b/lib/backbone-pageable.min.js
@@ -5,4 +5,4 @@
Copyright (c) 2013 Jimmy Yuen Ho Wong
Licensed under the MIT @license.
*/
-(function(e){if("object"==typeof exports)module.exports=e(require("underscore"),require("backbone"));else if("function"==typeof define&&define.amd)define(["underscore","backbone"],e);else if("undefined"!=typeof _&&"undefined"!=typeof Backbone){var t=Backbone.PageableCollection,r=Backbone.PageableCollection=e(_,Backbone);Backbone.PageableCollection.noConflict=function(){return Backbone.PageableCollection=t,r}}})(function(e,t){"use strict";function r(t,r){if(t*=1,!e.isNumber(t)||e.isNaN(t)||!e.isFinite(t)||~~t!==t)throw new TypeError("`"+r+"` must be a finite integer");return t}function a(e){for(var t,r,a,s,i={},n=decodeURIComponent,o=e.split("&"),l=0,c=o.length;c>l;l++){var u=o[l];t=u.split("="),r=t[0],a=t[1]||!0,r=n(r),s=i[r],d(s)?s.push(a):i[r]=s?[s,a]:a}return i}function s(){var t=arguments[0],r=e.toArray(arguments).slice(1),a=t.comparator;t.comparator=null;try{t.reset.apply(t,r)}finally{t.comparator=a,a&&t.sort()}return t}var i=e.extend,n=e.omit,o=e.clone,l=e.each,c=e.pick,u=e.contains,f=e.isEmpty,h=e.pairs,g=e.invert,d=e.isArray,v=e.isFunction,P=e.isObject,p=e.keys,m=e.isUndefined,y=e.result,k=Math.ceil,b=Math.max,S=t.Collection.prototype,_=/[\s'"]/g,C=/[<>\s'"]/g,R=t.Collection.extend({state:{firstPage:1,lastPage:null,currentPage:null,pageSize:25,totalPages:null,totalRecords:null,sortKey:null,order:-1},mode:"server",queryParams:{currentPage:"page",pageSize:"per_page",totalPages:"total_pages",totalRecords:"total_entries",sortKey:"sort_by",order:"order",directions:{"-1":"asc",1:"desc"}},initialize:function(e,t){t=t||{};var r=this.mode=t.mode||this.mode||x.mode,a=i({},x.queryParams,this.queryParams,t.queryParams||{});a.directions=i({},x.queryParams.directions,this.queryParams.directions,a.directions||{}),this.queryParams=a;var s=this.state=i({},x.state,this.state,t.state||{});s.currentPage=null==s.currentPage?s.firstPage:s.currentPage,"server"==r||null!=s.totalRecords||f(e)||(s.totalRecords=e.length),this.switchMode(r,i({fetch:!1,resetState:!1,models:e},t));var n=t.comparator;if(s.sortKey&&!n&&this.setSorting(s.sortKey,s.order,t),"server"!=r){if(n&&t.full){delete this.comparator;var l=this.fullCollection;l.comparator=n,l.sort()}e&&!f(e)&&(this.getPage(s.currentPage),e.splice.apply(e,[0,e.length].concat(this.models)))}this._initState=o(this.state)},_makeFullCollection:function(e,r){var a,s,i,n=["url","model","sync","comparator"],o=this.constructor.prototype,l={};for(a=0,s=n.length;s>a;a++)i=n[a],m(o[i])||(l[i]=o[i]);var c=new(t.Collection.extend(l))(e,r);for(a=0,s=n.length;s>a;a++)i=n[a],this[i]!==o[i]&&(c[i]=this[i]);return c},_makeCollectionEventHandler:function(e,t){return function(r,a,n,c){var u=e._handlers;l(p(u),function(r){var a=u[r];e.off(r,a),t.off(r,a)});var f=o(e.state),h=f.firstPage,g=0===h?f.currentPage:f.currentPage-1,d=f.pageSize,v=g*d,P=v+d;if("add"==r){var y,b,S,_,c=c||{};if(n==t)b=t.indexOf(a),b>=v&&P>b&&(_=e,y=S=b-v);else{y=e.indexOf(a),b=v+y,_=t;var S=m(c.at)?b:c.at+v}if(++f.totalRecords,e.state=e._checkState(f),_){_.add(a,i({},c||{},{at:S}));var C=y>=d?a:!m(c.at)&&P>S&&e.length>d?e.at(d):null;if(C){var R=n._events.add||[],x={onAdd:!0};if(R.length){var w=R[R.length-1],q=w.callback;w.callback=function(){try{q.apply(this,arguments),e.remove(C,x)}finally{w.callback=q}}}else e.remove(C,x)}}}if("remove"==r)if(c.onAdd)delete c.onAdd;else{if(--f.totalRecords){var z=f.totalPages=k(f.totalRecords/d);f.lastPage=0===h?z-1:z,f.currentPage>z&&(f.currentPage=f.lastPage)}else f.totalRecords=null,f.totalPages=null;e.state=e._checkState(f);var E,K=c.index;n==e?((E=t.at(P))&&e.push(E),t.remove(a)):K>=v&&P>K&&(e.remove(a),E=t.at(g*(d+K)),E&&e.push(E))}if("reset"==r||"sort"==r){if(c=n,n=a,n==e&&"reset"==r){var N=t.models.slice(0,v),B=t.models.slice(v+e.models.length);c=i(c,{silent:!0}),s(t,N.concat(e.models).concat(B),c)}("reset"==r||n==t)&&((f.totalRecords=t.models.length)||(f.totalRecords=null,f.totalPages=null,f.lastPage=f.currentPage=f.firstPage),e.state=e._checkState(f),n==e&&t.trigger(r,t,c),s(e,t.models.slice(v,P),c))}l(p(u),function(r){var a=u[r];l([e,t],function(e){e.on(r,a);var t=e._events[r]||[];t.unshift(t.pop())})})}},_checkState:function(e){var t=this.mode,a=this.links,s=e.totalRecords,i=e.pageSize,n=e.currentPage,o=e.firstPage,l=e.totalPages;if(null!=s&&null!=i&&null!=n&&null!=o&&("infinite"==t?a:!0)){if(s=r(s,"totalRecords"),i=r(i,"pageSize"),n=r(n,"currentPage"),o=r(o,"firstPage"),1>i)throw new RangeError("`pageSize` must be >= 1");if(l=e.totalPages=k(s/i),0>o||o>1)throw new RangeError("`firstPage must be 0 or 1`");if(e.lastPage=0===o?b(0,l-1):l,"infinite"==t){if(!a[n+""])throw new RangeError("No link found for page "+n)}else{if(0===o&&(o>n||n>=l&&l>0))throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got "+n+".");if(1===o&&(o>n||n>l))throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got "+n+".")}}return e},setPageSize:function(e,t){return e=r(e,"pageSize"),t=t||{},this.state=this._checkState(i({},this.state,{pageSize:e,totalPages:k(this.state.totalRecords/e)})),this.getPage(this.state.currentPage,t)},switchMode:function(t,r){if(!u(["server","client","infinite"],t))throw new TypeError('`mode` must be one of "server", "client" or "infinite"');r=r||{fetch:!0,resetState:!0};var a=this.state=r.resetState?o(this._initState):this._checkState(i({},this.state));this.mode=t;var s,c=this,f=this.fullCollection,h=this._handlers=this._handlers||{};if("server"==t||f)"server"==t&&f&&(l(p(h),function(e){s=h[e],c.off(e,s),f.off(e,s)}),delete this._handlers,this._fullComparator=f.comparator,delete this.fullCollection);else{f=this._makeFullCollection(r.models||[]),f.pageableCollection=this,this.fullCollection=f;var g=this._makeCollectionEventHandler(this,f);l(["add","remove","reset","sort"],function(t){h[t]=s=e.bind(g,{},t),c.on(t,s),f.on(t,s)}),f.comparator=this._fullComparator}if("infinite"==t)for(var d=this.links={},v=a.firstPage,P=k(a.totalRecords/a.pageSize),m=0===v?b(0,P-1):P||v,y=a.firstPage;m>=y;y++)d[y]=this.url;else this.links&&delete this.links;return r.fetch?this.fetch(n(r,"fetch","resetState")):this},hasPrevious:function(){var e=this.state,t=e.currentPage;return"infinite"!=this.mode?t>e.firstPage:!!this.links[t-1]},hasNext:function(){var e=this.state,t=this.state.currentPage;return"infinite"!=this.mode?e.lastPage>t:!!this.links[t+1]},getFirstPage:function(e){return this.getPage("first",e)},getPreviousPage:function(e){return this.getPage("prev",e)},getNextPage:function(e){return this.getPage("next",e)},getLastPage:function(e){return this.getPage("last",e)},getPage:function(e,t){var a=this.mode,o=this.fullCollection;t=t||{fetch:!1};var l=this.state,c=l.firstPage,u=l.currentPage,h=l.lastPage,g=l.pageSize,d=e;switch(e){case"first":d=c;break;case"prev":d=u-1;break;case"next":d=u+1;break;case"last":d=h;break;default:d=r(e,"index")}this.state=this._checkState(i({},l,{currentPage:d}));var v=(0===c?d:d-1)*g,P=o&&o.length?o.models.slice(v,v+g):[];return"client"!=a&&("infinite"!=a||f(P))||t.fetch?("infinite"==a&&(t.url=this.links[d]),this.fetch(n(t,"fetch"))):s(this,P,n(t,"fetch"))},sync:function(e,r,a){var s=this;if("infinite"==s.mode){var n=a.success,o=s.state.currentPage;a.success=function(e,t,r){var l=s.links,c=s.parseLinks(e,i({xhr:r},a));c.first&&(l[s.state.firstPage]=c.first),c.prev&&(l[o-1]=c.prev),c.next&&(l[o+1]=c.next),n&&n(e,t,r)}}return(S.sync||t.sync).call(s,e,r,a)},parseLinks:function(e,t){var r={},s=t.xhr.getResponseHeader("Link");if(s){var i=["first","prev","previous","next","last"];l(s.split(","),function(e){var t=e.split(";"),a=t[0].replace(C,""),s=t.slice(1);l(s,function(e){var t=e.split("="),s=t[0].replace(_,""),n=t[1].replace(_,"");"rel"==s&&u(i,n)&&("previous"==n?r.prev=a:r[n]=a)})});var n,c,f=r.last||"";if(c=(n=f.indexOf("?"))?f.slice(n+1):""){var h=a(c),g=o(this.state),d=this.queryParams,v=g.pageSize,P=1*h[d.totalRecords],p=1*h[d.currentPage],m=h[d.totalPages];P||(p?P=(0===g.firstPage?p+1:p)*v:m&&(P=m*v)),P&&(g.totalRecords=P),this.state=this._checkState(g)}}return delete r.last,r},parse:function(e){var t=this.parseState(e,o(this.queryParams),o(this.state));return t&&(this.state=this._checkState(i({},this.state,t))),this.parseRecords(e)},parseState:function(t,r,a){if(t&&2===t.length&&P(t[0])&&d(t[1])){var s=o(a),i=t[0];return l(h(n(r,"directions")),function(t){var r=t[0],a=t[1],n=i[a];m(n)||e.isNull(n)||(s[r]=i[a])}),i.order&&(s.order=1*g(r.directions)[i.order]),s}},parseRecords:function(e){return e&&2===e.length&&P(e[0])&&d(e[1])?e[1]:e},fetch:function(e){e=e||{};var t=this._checkState(this.state),r=this.mode;"infinite"!=r||e.url||(e.url=this.links[t.currentPage]);var l=e.data||{},u=y(e,"url")||y(this,"url")||"",f=u.indexOf("?");-1!=f&&(i(l,a(u.slice(f+1))),u=u.slice(0,f)),e.url=u,e.data=l;var g,d,P,k,b="client"==this.mode?c(this.queryParams,"sortKey","order"):n(c(this.queryParams,p(x.queryParams)),"directions"),_=h(b),C=o(this);for(g=0;_.length>g;g++)d=_[g],P=d[0],k=d[1],k=v(k)?k.call(C):k,null!=t[P]&&null!=k&&(l[k]=t[P]);t.sortKey&&t.order?l[b.order]=this.queryParams.directions[t.order+""]:t.sortKey||delete l[b.order];var R=h(n(this.queryParams,p(x.queryParams)));for(g=0;R.length>g;g++)d=R[g],k=d[1],k=v(k)?k.call(C):k,l[d[0]]=k;var w=this.fullCollection,q=this.links;if("server"!=r){var z=this,E=e.success;return e.success=function(a,n,o){o=o||{},m(e.silent)?delete o.silent:o.silent=e.silent;var l=a.models,c=t.currentPage;if("client"==r)s(w,l,o);else if(q[c]){var u=t.pageSize,f=(0===t.firstPage?c:c-1)*u,h=w.models,g=h.slice(0,f),d=h.slice(f+u);h=g.concat(l).concat(d);var v=w.set||w.update;v.call(w,h,i({silent:!0,sort:!1},o)),w.comparator&&w.sort(),w.trigger("reset",w,o)}else w.add(l,i({at:w.length,silent:!0},o)),w.trigger("reset",w,o);E&&E(a,n,o)},S.fetch.call(z,i({},e,{silent:!0}))}return S.fetch.call(this,e)},_makeComparator:function(e,t){var r=this.state;return e=e||r.sortKey,t=t||r.order,e&&t?function(r,a){var s,i=r.get(e),n=a.get(e);return 1===t&&(s=i,i=n,n=s),i===n?0:n>i?-1:1}:void 0},setSorting:function(e,t,r){var a=this.state;a.sortKey=e,a.order=t=t||a.order;var s=this.fullCollection,n=!1,o=!1;e||(n=o=!0);var l=this.mode;r=i({side:"client"==l?l:"server",full:!0},r);var c=this._makeComparator(e,t),u=r.full,f=r.side;return"client"==f?u?(s&&(s.comparator=c),n=!0):(this.comparator=c,o=!0):"server"!=f||u||(this.comparator=c),n&&delete this.comparator,o&&s&&delete s.comparator,this}}),x=R.prototype;return R});
\ No newline at end of file
+(function(e){if("object"==typeof exports)module.exports=e(require("underscore"),require("backbone"));else if("function"==typeof define&&define.amd)define(["underscore","backbone"],e);else if("undefined"!=typeof _&&"undefined"!=typeof Backbone){var t=Backbone.PageableCollection,r=Backbone.PageableCollection=e(_,Backbone);Backbone.PageableCollection.noConflict=function(){return Backbone.PageableCollection=t,r}}})(function(e,t){"use strict";function r(t,r){if(!e.isNumber(t)||e.isNaN(t)||!e.isFinite(t)||~~t!==t)throw new TypeError("`"+r+"` must be a finite integer");return t}function a(e){for(var t,r,a,s,i={},n=decodeURIComponent,o=e.split("&"),l=0,c=o.length;c>l;l++){var u=o[l];t=u.split("="),r=t[0],a=t[1]||!0,r=n(r),s=i[r],d(s)?s.push(a):i[r]=s?[s,a]:a}return i}function s(){var t=arguments[0],r=e.toArray(arguments).slice(1),a=t.comparator;t.comparator=null;try{t.reset.apply(t,r)}finally{t.comparator=a,a&&t.sort()}return t}var i=e.extend,n=e.omit,o=e.clone,l=e.each,c=e.pick,u=e.contains,f=e.isEmpty,h=e.pairs,g=e.invert,d=e.isArray,p=e.isFunction,v=e.isObject,P=e.keys,m=e.isUndefined,y=e.result,k=Math.ceil,b=Math.max,S=t.Collection.prototype,_=/[\s'"]/g,C=/[<>\s'"]/g,R=t.Collection.extend({state:{firstPage:1,lastPage:null,currentPage:null,pageSize:25,totalPages:null,totalRecords:null,sortKey:null,order:-1},mode:"server",queryParams:{currentPage:"page",pageSize:"per_page",totalPages:"total_pages",totalRecords:"total_entries",sortKey:"sort_by",order:"order",directions:{"-1":"asc",1:"desc"}},initialize:function(e,t){t=t||{};var r=this.mode=t.mode||this.mode||x.mode,a=i({},x.queryParams,this.queryParams,t.queryParams||{});a.directions=i({},x.queryParams.directions,this.queryParams.directions,a.directions||{}),this.queryParams=a;var s=this.state=i({},x.state,this.state,t.state||{});s.currentPage=null==s.currentPage?s.firstPage:s.currentPage,"server"==r||null!=s.totalRecords||f(e)||(s.totalRecords=e.length),this.switchMode(r,i({fetch:!1,resetState:!1,models:e},t));var n=t.comparator;if(s.sortKey&&!n&&this.setSorting(s.sortKey,s.order,t),"server"!=r){if(n&&t.full){delete this.comparator;var l=this.fullCollection;l.comparator=n,l.sort()}e&&!f(e)&&(this.getPage(s.currentPage),e.splice.apply(e,[0,e.length].concat(this.models)))}this._initState=o(this.state)},_makeFullCollection:function(e,r){var a,s,i,n=["url","model","sync","comparator"],o=this.constructor.prototype,l={};for(a=0,s=n.length;s>a;a++)i=n[a],m(o[i])||(l[i]=o[i]);var c=new(t.Collection.extend(l))(e,r);for(a=0,s=n.length;s>a;a++)i=n[a],this[i]!==o[i]&&(c[i]=this[i]);return c},_makeCollectionEventHandler:function(e,t){return function(r,a,n,c){var u=e._handlers;l(P(u),function(r){var a=u[r];e.off(r,a),t.off(r,a)});var f=o(e.state),h=f.firstPage,g=0===h?f.currentPage:f.currentPage-1,d=f.pageSize,p=g*d,v=p+d;if("add"==r){var y,b,S,_,c=c||{};if(n==t)b=t.indexOf(a),b>=p&&v>b&&(_=e,y=S=b-p);else{y=e.indexOf(a),b=p+y,_=t;var S=m(c.at)?b:c.at+p}if(++f.totalRecords,e.state=e._checkState(f),_){_.add(a,i({},c||{},{at:S}));var C=y>=d?a:!m(c.at)&&v>S&&e.length>d?e.at(d):null;if(C){var R=n._events.add||[],x={onAdd:!0};if(R.length){var w=R[R.length-1],q=w.callback;w.callback=function(){try{q.apply(this,arguments),e.remove(C,x)}finally{w.callback=q}}}else e.remove(C,x)}}}if("remove"==r)if(c.onAdd)delete c.onAdd;else{if(--f.totalRecords){var z=f.totalPages=k(f.totalRecords/d);f.lastPage=0===h?z-1:z,f.currentPage>z&&(f.currentPage=f.lastPage)}else f.totalRecords=null,f.totalPages=null;e.state=e._checkState(f);var E,K=c.index;n==e?((E=t.at(v))&&e.push(E),t.remove(a)):K>=p&&v>K&&(e.remove(a),E=t.at(g*(d+K)),E&&e.push(E))}if("reset"==r||"sort"==r){if(c=n,n=a,n==e&&"reset"==r){var N=t.models.slice(0,p),B=t.models.slice(p+e.models.length);c=i(c,{silent:!0}),s(t,N.concat(e.models).concat(B),c)}("reset"==r||n==t)&&((f.totalRecords=t.models.length)||(f.totalRecords=null,f.totalPages=null,f.lastPage=f.currentPage=f.firstPage),e.state=e._checkState(f),n==e&&t.trigger(r,t,c),s(e,t.models.slice(p,v),i({},c,{parse:!1})))}l(P(u),function(r){var a=u[r];l([e,t],function(e){e.on(r,a);var t=e._events[r]||[];t.unshift(t.pop())})})}},_checkState:function(e){var t=this.mode,a=this.links,s=e.totalRecords,i=e.pageSize,n=e.currentPage,o=e.firstPage,l=e.totalPages;if(null!=s&&null!=i&&null!=n&&null!=o&&("infinite"==t?a:!0)){if(s=r(s,"totalRecords"),i=r(i,"pageSize"),n=r(n,"currentPage"),o=r(o,"firstPage"),1>i)throw new RangeError("`pageSize` must be >= 1");if(l=e.totalPages=k(s/i),0>o||o>1)throw new RangeError("`firstPage must be 0 or 1`");if(e.lastPage=0===o?b(0,l-1):l,"infinite"==t){if(!a[n+""])throw new RangeError("No link found for page "+n)}else{if(0===o&&(o>n||n>=l&&l>0))throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got "+n+".");if(1===o&&(o>n||n>l&&l>0))throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got "+n+".")}}return e},setPageSize:function(e,t){return e=r(e,"pageSize"),t=t||{},this.state=this._checkState(i({},this.state,{pageSize:e,totalPages:k(this.state.totalRecords/e)})),this.getPage(this.state.currentPage,t)},switchMode:function(t,r){if(!u(["server","client","infinite"],t))throw new TypeError('`mode` must be one of "server", "client" or "infinite"');r=r||{fetch:!0,resetState:!0};var a=this.state=r.resetState?o(this._initState):this._checkState(i({},this.state));this.mode=t;var s,c=this,f=this.fullCollection,h=this._handlers=this._handlers||{};if("server"==t||f)"server"==t&&f&&(l(P(h),function(e){s=h[e],c.off(e,s),f.off(e,s)}),delete this._handlers,this._fullComparator=f.comparator,delete this.fullCollection);else{f=this._makeFullCollection(r.models||[]),f.pageableCollection=this,this.fullCollection=f;var g=this._makeCollectionEventHandler(this,f);l(["add","remove","reset","sort"],function(t){h[t]=s=e.bind(g,{},t),c.on(t,s),f.on(t,s)}),f.comparator=this._fullComparator}if("infinite"==t)for(var d=this.links={},p=a.firstPage,v=k(a.totalRecords/a.pageSize),m=0===p?b(0,v-1):v||p,y=a.firstPage;m>=y;y++)d[y]=this.url;else this.links&&delete this.links;return r.fetch?this.fetch(n(r,"fetch","resetState")):this},hasPrevious:function(){var e=this.state,t=e.currentPage;return"infinite"!=this.mode?t>e.firstPage:!!this.links[t-1]},hasNext:function(){var e=this.state,t=this.state.currentPage;return"infinite"!=this.mode?e.lastPage>t:!!this.links[t+1]},getFirstPage:function(e){return this.getPage("first",e)},getPreviousPage:function(e){return this.getPage("prev",e)},getNextPage:function(e){return this.getPage("next",e)},getLastPage:function(e){return this.getPage("last",e)},getPage:function(e,t){var a=this.mode,o=this.fullCollection;t=t||{fetch:!1};var l=this.state,c=l.firstPage,u=l.currentPage,h=l.lastPage,g=l.pageSize,d=e;switch(e){case"first":d=c;break;case"prev":d=u-1;break;case"next":d=u+1;break;case"last":d=h;break;default:d=r(e,"index")}this.state=this._checkState(i({},l,{currentPage:d}));var p=(0===c?d:d-1)*g,v=o&&o.length?o.models.slice(p,p+g):[];return"client"!=a&&("infinite"!=a||f(v))||t.fetch?("infinite"==a&&(t.url=this.links[d]),this.fetch(n(t,"fetch"))):s(this,v,n(t,"fetch"))},sync:function(e,r,a){var s=this;if("infinite"==s.mode){var n=a.success,o=s.state.currentPage;a.success=function(e,t,r){var l=s.links,c=s.parseLinks(e,i({xhr:r},a));c.first&&(l[s.state.firstPage]=c.first),c.prev&&(l[o-1]=c.prev),c.next&&(l[o+1]=c.next),n&&n(e,t,r)}}return(S.sync||t.sync).call(s,e,r,a)},parseLinks:function(e,t){var r={},s=t.xhr.getResponseHeader("Link");if(s){var i=["first","prev","previous","next","last"];l(s.split(","),function(e){var t=e.split(";"),a=t[0].replace(C,""),s=t.slice(1);l(s,function(e){var t=e.split("="),s=t[0].replace(_,""),n=t[1].replace(_,"");"rel"==s&&u(i,n)&&("previous"==n?r.prev=a:r[n]=a)})});var n,c,f=r.last||"";if(c=(n=f.indexOf("?"))?f.slice(n+1):""){var h=a(c),g=o(this.state),d=this.queryParams,p=g.pageSize,v=1*h[d.totalRecords],P=1*h[d.currentPage],m=h[d.totalPages];v||(P?v=(0===g.firstPage?P+1:P)*p:m&&(v=m*p)),v&&(g.totalRecords=v),this.state=this._checkState(g)}}return delete r.last,r},parse:function(e){var t=this.parseState(e,o(this.queryParams),o(this.state));return t&&(this.state=this._checkState(i({},this.state,t))),this.parseRecords(e)},parseState:function(t,r,a){if(t&&2===t.length&&v(t[0])&&d(t[1])){var s=o(a),i=t[0];return l(h(n(r,"directions")),function(t){var r=t[0],a=t[1],n=i[a];m(n)||e.isNull(n)||(s[r]=i[a])}),i.order&&(s.order=1*g(r.directions)[i.order]),s}},parseRecords:function(e){return e&&2===e.length&&v(e[0])&&d(e[1])?e[1]:e},fetch:function(e){e=e||{};var t=this._checkState(this.state),r=this.mode;"infinite"!=r||e.url||(e.url=this.links[t.currentPage]);var l=e.data||{},u=y(e,"url")||y(this,"url")||"",f=u.indexOf("?");-1!=f&&(i(l,a(u.slice(f+1))),u=u.slice(0,f)),e.url=u,e.data=l;var g,d,v,k,b="client"==this.mode?c(this.queryParams,"sortKey","order"):n(c(this.queryParams,P(x.queryParams)),"directions"),_=h(b),C=o(this);for(g=0;_.length>g;g++)d=_[g],v=d[0],k=d[1],k=p(k)?k.call(C):k,null!=t[v]&&null!=k&&(l[k]=t[v]);t.sortKey&&t.order?l[b.order]=this.queryParams.directions[t.order+""]:t.sortKey||delete l[b.order];var R=h(n(this.queryParams,P(x.queryParams)));for(g=0;R.length>g;g++)d=R[g],k=d[1],k=p(k)?k.call(C):k,l[d[0]]=k;var w=this.fullCollection,q=this.links;if("server"!=r){var z=this,E=e.success;return e.success=function(a,n,o){o=o||{},m(e.silent)?delete o.silent:o.silent=e.silent;var l=a.models,c=t.currentPage;if("client"==r)s(w,l,o);else if(q[c]){var u=t.pageSize,f=(0===t.firstPage?c:c-1)*u,h=w.models,g=h.slice(0,f),d=h.slice(f+u);h=g.concat(l).concat(d);var p=w.set||w.update;p.call(w,h,i({silent:!0,sort:!1},o)),w.comparator&&w.sort(),w.trigger("reset",w,o)}else w.add(l,i({at:w.length,silent:!0},o)),w.trigger("reset",w,o);E&&E(a,n,o)},S.fetch.call(z,i({},e,{silent:!0}))}return S.fetch.call(this,e)},_makeComparator:function(e,t){var r=this.state;return e=e||r.sortKey,t=t||r.order,e&&t?function(r,a){var s,i=r.get(e),n=a.get(e);return 1===t&&(s=i,i=n,n=s),i===n?0:n>i?-1:1}:void 0},setSorting:function(e,t,r){var a=this.state;a.sortKey=e,a.order=t=t||a.order;var s=this.fullCollection,n=!1,o=!1;e||(n=o=!0);var l=this.mode;r=i({side:"client"==l?l:"server",full:!0},r);var c=this._makeComparator(e,t),u=r.full,f=r.side;return"client"==f?u?(s&&(s.comparator=c),n=!0):(this.comparator=c,o=!0):"server"!=f||u||(this.comparator=c),n&&delete this.comparator,o&&s&&delete s.comparator,this}}),x=R.prototype;return R});
\ No newline at end of file
diff --git a/test/server-pageable.js b/test/server-pageable.js
index ee1a01c..3727f03 100644
--- a/test/server-pageable.js
+++ b/test/server-pageable.js
@@ -134,6 +134,12 @@ $(document).ready(function () {
state.totalRecords = 0;
col._checkState(state);
strictEqual(state.lastPage, 0);
+
+ state.firstPage = 1;
+ state.currentPage = 1;
+ state.totalRecords = 0;
+ col._checkState(state);
+ strictEqual(state.lastPage, 0);
});
test("extend and initialize", function () {
@@ -419,13 +425,7 @@ $(document).ready(function () {
sinon.stub(col, "getPage");
- col.setPageSize(200);
- strictEqual(col.state.pageSize, 200);
- strictEqual(col.getPage.args.length, 1);
- strictEqual(col.getPage.args[0][0], 1);
- col.getPage.reset();
-
- col.setPageSize("50");
+ col.setPageSize(50);
strictEqual(col.state.pageSize, 50);
strictEqual(col.getPage.args.length, 1);
strictEqual(col.getPage.args[0][0], 1);