Skip to content

Commit

Permalink
Merge branch 'master' of github.com:catarse/mithril.postgrest
Browse files Browse the repository at this point in the history
  • Loading branch information
diogob committed Oct 7, 2015
2 parents bfffd70 + fd8e33c commit 0a65bbf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
20 changes: 16 additions & 4 deletions dist/mithril.postgrest.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mithril.postgrest.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions spec/paginationVM.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ describe("m.postgrest.paginationVM", function(){

describe("when fetch fails", function(){
beforeEach(function(){
vm = m.postgrest.paginationVM(model.getPage);
vm = m.postgrest.paginationVM(model, null, false);
jasmine.Ajax.stubRequest(/foo.*/).andReturn({
'status' : 401,
'status' : 401,
'responseText' : 'Invalid user'
});
});
Expand All @@ -35,9 +35,9 @@ describe("m.postgrest.paginationVM", function(){
describe("when fetch is successful", function(){
beforeEach(function(){
spyOn(model, "getPage").and.callThrough();
vm = m.postgrest.paginationVM(model.getPage);
vm = m.postgrest.paginationVM(model, null, false);
jasmine.Ajax.stubRequest(/foo.*/).andReturn({
'responseText' : '["items"]'
'responseText' : '["items"]',
});
});

Expand Down Expand Up @@ -65,6 +65,7 @@ describe("m.postgrest.paginationVM", function(){

describe("#firstPage", function() {
it("should be a function", function(){
var lastRequest = jasmine.Ajax.requests.mostRecent();
expect(vm.firstPage).toBeFunction();
});

Expand All @@ -80,6 +81,17 @@ describe("m.postgrest.paginationVM", function(){
});
});

describe("#isLastPage", function() {
it("should be a function", function() {
expect(vm.isLastPage).toBeFunction();
});

it("should return false when is not the last page", function() {
vm.firstPage();
expect(vm.isLastPage()).toEqual(false);
});
});

describe("#nextPage", function() {
it("should be a function", function(){
expect(vm.nextPage).toBeFunction();
Expand Down
17 changes: 14 additions & 3 deletions src/vms/paginationVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
factory(window.m, window._);
}
}(function(m, _) {
m.postgrest.paginationVM = (pageRequest, order) => {
m.postgrest.paginationVM = (model, order, authenticate = true) => {
let collection = m.prop([]),
rangeHeaderRgx = new RegExp("(\d+)-(\d+)?\/?(\d+)"),
defaultOrder = order || 'id.desc',
filters = m.prop({order: defaultOrder}),
isLoading = m.prop(false),
page = m.prop(1),
resultsCount = m.prop(10),
pageRequest = authenticate ? model.getPageWithToken : model.getPage,
total = m.prop();

const fetch = () => {
Expand All @@ -22,8 +25,11 @@
return JSON.stringify({hint: null, details: null, code: 0, message: 'Connection error'});
}
let rangeHeader = xhr.getResponseHeader('Content-Range');
if (_.isString(rangeHeader) && rangeHeader.split('/').length > 1){
total(parseInt(rangeHeader.split('/')[1]));
if (_.isString(rangeHeader)){
let matches = rangeHeaderRgx.exec(rangeHeader);

total(parseInt(matches[2]));
resultsCount((parseInt(matches[0]) - parseInt(matches[1])));
}
try {
JSON.parse(xhr.responseText);
Expand Down Expand Up @@ -56,6 +62,10 @@
return fetch();
},

isLastPage = () => {
return (page() * model.pageSize() >= total());
},

nextPage = () => {
page(page() + 1);
return fetch();
Expand All @@ -66,6 +76,7 @@
firstPage: firstPage,
isLoading: isLoading,
nextPage: nextPage,
isLastPage: isLastPage,
total: total
};
};
Expand Down

0 comments on commit 0a65bbf

Please sign in to comment.