UNPKG

generator-landmark

Version:
87 lines (64 loc) 1.74 kB
var landmark = require('landmark-serve'), async = require('async'); exports = module.exports = function(req, res) { var view = new landmark.View(req, res), locals = res.locals; // Init locals locals.section = 'blog'; locals.filters = { category: req.params.category }; locals.data = { posts: [], categories: [] }; // Load all categories view.on('init', function(next) { landmark.list('PostCategory').model.find().sort('name').exec(function(err, results) { if (err || !results.length) { return next(err); } locals.data.categories = results; // Load the counts for each category async.each(locals.data.categories, function(category, next) { landmark.list('Post').model.count().where('category').in([category.id]).exec(function(err, count) { category.postCount = count; next(err); }); }, function(err) { next(err); }); }); }); // Load the current category filter view.on('init', function(next) { if (req.params.category) { landmark.list('PostCategory').model.findOne({ key: locals.filters.category }).exec(function(err, result) { locals.data.category = result; next(err); }); } else { next(); } }); // Load the posts view.on('init', function(next) { var q = landmark.list('Post').paginate({ page: req.query.page || 1, perPage: 10, maxPages: 10 }) .where('state', 'published') .sort('-publishedDate') .populate('author categories'); if (locals.data.category) { q.where('categories').in([locals.data.category]); } q.exec(function(err, results) { locals.data.posts = results; next(err); }); }); // Render the view view.render('blog'); };