Merge pull request #216 from cobbspur/pagination

closes #197 Pagination wiring
This commit is contained in:
Hannah Wolfe 2013-06-25 09:46:18 -07:00
commit b5f4c9334e
8 changed files with 90 additions and 16 deletions

1
app.js
View File

@ -156,6 +156,7 @@ when.all([ghost.init(), filters.loadCoreFilters(ghost), helpers.loadCoreHelpers(
*/
ghost.app().get('/:slug', frontend.single);
ghost.app().get('/', frontend.homepage);
ghost.app().get('/page/:page/', frontend.homepage);
ghost.app().listen(3333, function () {
console.log("Express server listening on port " + 3333);

@ -1 +1 @@
Subproject commit d0f18f78f7d0242509ab2cff7173dcef740edf12
Subproject commit 229ab708994f6c6a96909559755f55722a9f4ffd

View File

@ -32,6 +32,8 @@
this.currentPage = resp.page;
this.totalPages = resp.pages;
this.totalPosts = resp.total;
this.nextPage = resp.next;
this.prevPage = resp.prev;
return resp.posts;
}
return resp;

View File

@ -12,9 +12,10 @@ var Ghost = require('../../ghost'),
frontendControllers = {
'homepage': function (req, res) {
api.posts.browse().then(function (page) {
var page = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
api.posts.browse({page: page}).then(function (page) {
ghost.doFilter('prePostsRender', page.posts, function (posts) {
res.render('index', {posts: posts});
res.render('index', {posts: posts, pagination: {page: page.page, prev: page.prev, next: page.next, limit: page.limit, total: page.total, pages: page.pages}});
});
});
},

View File

@ -1,6 +1,7 @@
var _ = require('underscore'),
moment = require('moment'),
when = require('when'),
pagination = require('./paginate'),
navHelper = require('./ghostNav'),
hbs = require('express-hbs'),
coreHelpers;
@ -111,11 +112,11 @@ coreHelpers = function (ghost) {
}
return ret;
});
return when.all([
// Just one async helper for now, but could be more in the future
navHelper.registerWithGhost(ghost)
]);
// Just one async helper for now, but could be more in the future
return when.join(
navHelper.registerWithGhost(ghost),
pagination.registerWithGhost(ghost)
);
};
module.exports.loadCoreHelpers = coreHelpers;

View File

@ -0,0 +1,47 @@
var fs = require('fs'),
path = require('path'),
_ = require('underscore'),
handlebars = require('express-hbs').handlebars,
nodefn = require('when/node/function'),
PaginationHelper;
PaginationHelper = function (paginationTemplate) {
// Bind the context for our methods.
_.bindAll(this, 'compileTemplate', 'renderPagination');
if (_.isFunction(paginationTemplate)) {
this.paginationTemplateFunc = paginationTemplate;
} else {
this.paginationTemplatePath = paginationTemplate;
}
};
PaginationHelper.prototype.compileTemplate = function (templatePath) {
var self = this;
templatePath = templatePath || this.paginationTemplatePath;
return nodefn.call(fs.readFile, templatePath).then(function (paginationContents) {
// TODO: Can handlebars compile async?
self.paginationTemplateFunc = handlebars.compile(paginationContents.toString());
});
};
PaginationHelper.prototype.renderPagination = function (context, options) {
var output = this.paginationTemplateFunc(context);
return output;
};
PaginationHelper.registerWithGhost = function (ghost) {
var templatePath = path.join(ghost.paths().frontendViews, 'pagination.hbs'),
paginationHelper = new PaginationHelper(templatePath);
return paginationHelper.compileTemplate().then(function () {
ghost.registerThemeHelper("paginate", paginationHelper.renderPagination);
});
};
module.exports = PaginationHelper;

View File

@ -0,0 +1,10 @@
<nav id="pagination" role="pagination">
{{#if next}}
<div class="previous-page"><a href="/page/{{next}}/">Older Posts →</a></div>
{{/if}}
<div class="page-number">Page {{page}}<span class="extended"> of {{pages}}</span></div>
{{#if prev}}
<div class="next-page"><a href="/page/{{prev}}/">← Newer Posts</a></div></nav>
{{/if}}
</nav>

View File

@ -140,14 +140,26 @@ Post = GhostBookshelf.Model.extend({
}
return qb.count(_.result(collection, 'idAttribute')).then(function (resp) {
var totalPosts = resp[0].aggregate;
return {
posts: collection.toJSON(),
page: opts.page,
limit: opts.limit,
pages: Math.ceil(totalPosts / opts.limit),
total: totalPosts
};
var totalPosts = resp[0].aggregate,
data = {
posts: collection.toJSON(),
page: opts.page,
limit: opts.limit,
pages: Math.ceil(totalPosts / opts.limit),
total: totalPosts
};
if (data.pages > 1) {
if (data.page === 1) {
data.next = data.page + 1;
} else if (data.page === data.pages) {
data.prev = data.page - 1;
} else {
data.next = data.page + 1;
data.prev = data.page - 1;
}
}
return data;
}, errors.logAndThrowError);
}, errors.logAndThrowError);
},