Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var UsersController = Class('UsersController').inherits(BaseController)({
- beforeActions : [
- {
- before : ['_loadUser'], \\ An array of functions or current controller method names.
- actions : ['show', 'edit', 'update', 'destroy']
- }
- ],
- prototype : {
- _loadUser : function(req, res, next) {
- User.query().where('id', req.params.id).then(function(result) {
- res.locals.user = result[0];
- next()
- });
- },
- show: function(req, res, next) {
- res.format({
- json : function() {
- res.json(res.locals.user) // _loadUser method ran after show method, so req.locals.user is available here
- },
- html: function() {
- res.render('user/show.html') // same here, req.locals.user is available in the template.
- }
- });
- }
- }
- });
Add Comment
Please, Sign In to add comment