Guest User

Untitled

a guest
May 24th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. var UsersController = Class('UsersController').inherits(BaseController)({
  2. beforeActions : [
  3. {
  4. before : ['_loadUser'], \\ An array of functions or current controller method names.
  5. actions : ['show', 'edit', 'update', 'destroy']
  6. }
  7. ],
  8. prototype : {
  9. _loadUser : function(req, res, next) {
  10. User.query().where('id', req.params.id).then(function(result) {
  11. res.locals.user = result[0];
  12. next()
  13. });
  14. },
  15. show: function(req, res, next) {
  16. res.format({
  17. json : function() {
  18. res.json(res.locals.user) // _loadUser method ran after show method, so req.locals.user is available here
  19. },
  20. html: function() {
  21. res.render('user/show.html') // same here, req.locals.user is available in the template.
  22. }
  23. });
  24. }
  25. }
  26. });
Add Comment
Please, Sign In to add comment