From 4209b858ecdbfd9a354d429883dfd86c4df4cf41 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 4 Jul 2012 14:32:29 +0200 Subject: o Adding paging when rendering c+j for department list. --- routes/index.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'routes/index.js') diff --git a/routes/index.js b/routes/index.js index 8e4c28f..de7e0b1 100644 --- a/routes/index.js +++ b/routes/index.js @@ -36,6 +36,8 @@ function pager(req, count) { pageSize: pageSize, offset: offset, limit: limit, + page: offset / pageSize + 1, + pageCount: Math.round(count / pageSize), count: count, prevOffset: prevOffset >= 0 ? prevOffset : undefined, nextOffset: nextOffset < count ? nextOffset : undefined @@ -94,6 +96,25 @@ function send_cj(req, res, c) { } } +function linksFromPager(req, p, urlgenerator) { + var links = []; + if(_.isNumber(p.prevOffset)) { + links.push({ + href: urlgenerator(_.extend({}, req.query, {offset: p.prevOffset})), + rel: 'prev', + prompt: 'Previous page (' + (p.page - 1) + ' of ' + p.pageCount + ')' + }); + } + if(_.isNumber(p.nextOffset)) { + links.push({ + href: urlgenerator(_.extend({}, req.query, {offset: p.nextOffset})), + rel: 'next', + prompt: 'Next page (' + (p.page + 1) + ' of ' + p.pageCount + ')' + }); + } + return links; +} + function getIndex(req, res) { switch(req.accept.types.getBestMatch(['text/html', 'application/vnd.collection+json'])) { case 'text/html': @@ -146,7 +167,8 @@ function getDepartments(req, res) { case '*/*': var c = {collection: { href: res.urlgenerator.departments(), - items: mapDepartments(res, rs2.rows) + items: mapDepartments(res, rs2.rows), + links: linksFromPager(req, p, res.urlgenerator.departments), }}; send_cj(req, res, c); break; @@ -284,24 +306,9 @@ function getEmployees(req, res) { break; case 'application/vnd.collection+json': case '*/*': - var links = []; - if(_.isNumber(p.prevOffset)) { - links.push({ - href: res.urlgenerator.employees(_.extend({}, req.query, {offset: p.prevOffset})), - rel: 'prev', - prompt: 'Previous page' - }); - } - if(_.isNumber(p.nextOffset)) { - links.push({ - href: res.urlgenerator.employees(_.extend({}, req.query, {offset: p.nextOffset})), - rel: 'next', - prompt: 'Next page' - }); - } var c = {collection: { href: res.urlgenerator.employees(req.query), - links: links, + links: linksFromPager(req, p, res.urlgenerator.employees), queries: [{ href: res.urlgenerator.employees(), rel: 'search', -- cgit v1.2.3