apostrophe-snippets
Advanced tools
Comparing version 0.0.14 to 0.0.15
82
index.js
@@ -833,3 +833,3 @@ var async = require('async'); | ||
function permissions(callback) { | ||
// Does self person have any business editing snippets? If so make that | ||
// Does this person have any business editing snippets? If so make that | ||
// fact available to templates so they can offer buttons to access | ||
@@ -869,34 +869,40 @@ // the admin interface conveniently | ||
// | ||
// Often overridden when subclassing. For instance, the blog places the publication | ||
// date in the URL before the slug of the post, just to make things feel bloggier. | ||
// | ||
// It's not uncommon to override this completely, but before you do that, look at | ||
// the isShow, show, index and addCriteria methods to see if overriding them is enough | ||
// for your needs. | ||
self.dispatch = function(req, callback) { | ||
var permalink = false; | ||
var criteria = {}; | ||
if (req.remainder.length) { | ||
// Perhaps it's a snippet permalink | ||
criteria.slug = req.remainder.substr(1); | ||
permalink = true; | ||
var show = false; | ||
var slug = self.isShow(req); | ||
if (slug !== false) { | ||
show = true; | ||
criteria.slug = slug; | ||
} | ||
self.addCriteria(req, criteria); | ||
self.get(req, criteria, function(err, snippets) { | ||
// If we are requesting a specific slug, remove the tags criterion. | ||
// In theory we should be strict about this, but in practice this is | ||
// sometimes necessary to make sure permalink pages are available when | ||
// users have not created any really appropriate snippet page. TODO: | ||
// consider whether to go back to being strict, after we resolve | ||
// any concerns with DR. | ||
if (slug) { | ||
criteria.tags = undefined; | ||
} | ||
return self.get(req, criteria, function(err, snippets) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (permalink) { | ||
if (show) { | ||
if (!snippets.length) { | ||
req.template = 'notfound'; | ||
// Correct way to request a 404 from a loader. | ||
// Other loaders could still override this, which is good | ||
req.notfound = true; | ||
return callback(null); | ||
} else { | ||
req.template = self.renderer('show'); | ||
// Generic noun so we can more easily inherit templates | ||
req.extras.item = snippets[0]; | ||
req.extras.item.url = self.permalink(req.extras.item, req.bestPage); | ||
return self.show(req, snippets[0], callback); | ||
} | ||
} else { | ||
req.template = self.renderer('index'); | ||
// Generic noun so we can more easily inherit templates | ||
req.extras.items = snippets; | ||
_.each(snippets, function(snippet) { | ||
snippet.url = self.permalink(snippet, req.bestPage); | ||
}); | ||
return self.index(req, snippets, callback); | ||
} | ||
@@ -907,2 +913,36 @@ return callback(null); | ||
// If this request looks like a request for a 'show' page (a permalink), | ||
// this method returns the expected snippet slug. Otherwise it returns | ||
// false. Override this to match URLs with extra vanity components like | ||
// the publication date in an article URL. | ||
self.isShow = function(req) { | ||
if (req.remainder.length) { | ||
// Perhaps it's a snippet permalink | ||
return req.remainder.substr(1); | ||
} | ||
return false; | ||
}; | ||
// The standard implementation of a 'show' page for a single snippet, for your | ||
// overriding convenience | ||
self.show = function(req, snippet, callback) { | ||
req.template = self.renderer('show'); | ||
// Generic noun so we can more easily inherit templates | ||
req.extras.item = snippet; | ||
req.extras.item.url = self.permalink(req.extras.item, req.bestPage); | ||
return callback(null); | ||
}; | ||
// The standard implementation of an 'index' page for many snippets, for your | ||
// overriding convenience | ||
self.index = function(req, snippets, callback) { | ||
req.template = self.renderer('index'); | ||
_.each(snippets, function(snippet) { | ||
snippet.url = self.permalink(snippet, req.bestPage); | ||
}); | ||
// Generic noun so we can more easily inherit templates | ||
req.extras.items = snippets; | ||
return callback(null); | ||
}; | ||
self.addCriteria = function(req, criteria) { | ||
@@ -909,0 +949,0 @@ if (req.page.typeSettings && req.page.typeSettings.tags && req.page.typeSettings.tags.length) { |
{ | ||
"name": "apostrophe-snippets", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Reusable content snippets for the Apostrophe content management system. The blog and events modules are built on this foundation, which is also useful in and of itself.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
80708
1665