0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/server/controllers/preview.js
Hannah Wolfe 98f5ae00fc
Introduced renderer to DRY up controllers (#9235)
refs #5091, #9192

- Renderer figures out templates, contexts, and does a render call
- Templating is now handled with a single function
- Context call is made in the renderer

Note:  to make this work, all controllers now define a little bit of config, currently stored in res._route. (That's a totally temporary location, as is res._template... when a sensible naming convention reveals itself I'll get rid of the weird _). This exposes a type and for custom routes a template name & default.
2017-11-10 12:44:29 +00:00

48 lines
1.5 KiB
JavaScript

var api = require('../api'),
utils = require('../utils'),
filters = require('../filters'),
handleError = require('./frontend/error'),
renderEntry = require('./frontend/render-entry'),
setRequestIsSecure = require('./frontend/secure');
// This here is a controller.
// The "route" is handled in site/routes.js
module.exports = function previewController(req, res, next) {
var params = {
uuid: req.params.uuid,
status: 'all',
include: 'author,tags'
};
// Note: this is super similar to the config middleware used in channels
// @TODO refactor into to something explicit
res._route = {
type: 'entry'
};
api.posts.read(params).then(function then(result) {
// Format data 1
var post = result.posts[0];
if (!post) {
return next();
}
if (req.params.options && req.params.options.toLowerCase() === 'edit') {
// CASE: last param is of url is /edit, redirect to admin
return utils.url.redirectToAdmin(302, res, '#/editor/' + post.id);
} else if (req.params.options) {
// CASE: unknown options param detected. Ignore and end in 404.
return next();
}
if (post.status === 'published') {
return utils.url.redirect301(res, utils.url.urlFor('post', {post: post}));
}
setRequestIsSecure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(renderEntry(req, res));
}).catch(handleError(next));
};