More work on mvc example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 9231245 · Browse files · 7 files·+53 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+53 added−5 removed
- other:+53 added−5 removed
Provenance
- Commit
20c5892- Tree
7ababcf- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9231245
7 files changed+53 added−5 removed
modified:examples/mvc/controllers/app.js+2 added-2 removed
12344567789
module.exports = { // /users // / index: function(req, res){ res.render('index'); res.render(); }};No newline at end of fileNo newline at end of filemodified:examples/mvc/controllers/user.js+2 added-2 removed
9 unmodified lines1011121314141516171819202122232324252627289 unmodified lines // /users index: function(req, res){ res.send('users: ' + JSON.stringify(users)); res.render(users); },
// /users/:id
show: function(req, res, next){ var id = req.params.id, user = users[id]; if (user) { res.send('user: ' + JSON.stringify(user)); res.render(user); } else { next(new Error('User ' + id + ' does not exist')); } }};added:examples/mvc/views/app/index.ejs+5 added
1+<h1>Application Index</h1>2+<p>Try visiting one of the following urls:</p>3+<ul>4+ <li><a href="/users">/users</a></li>5+</ul>added:examples/mvc/views/user/index.ejs+6 added
1+<h1>Users</h1>2+<ul>3+ <% users.forEach(function(user){ %>4+ <li><%- user.name %></li>5+ <% }) %>6+</ul>added:examples/mvc/views/user/show.ejs+1 added
1+<h1>Viewing user <%- user.name %></h1>added:examples/mvc/views/layout.ejs+8 added
1+<html>2+ <head>3+ <title>Express - MVC Example</title>4+ </head>5+ <body>6+ <%- body %>7+ </body>8+</html>modified:examples/mvc/mvc.js+29 added-1 removed
25 unmodified lines262728293031323334353631373839404116 unmodified lines58596061626364656667686970717273747576777879808182838425 unmodified lines var name = file.replace('.js', ''), actions = require('./controllers/' + name), plural = name + 's', // realistically we would use an inflection lib prefix = '/' + plural;
// Special case for "app" if (name == 'app') { prefix = '/'; }
Object.keys(actions).map(function(action){ var fn = actions[action]; var fn = controllerAction(name, plural, action, actions[action]); switch(action) { case 'index': app.get(prefix, fn); break;16 unmodified lines app.del(prefix + '/:id', fn); break; } });}
// Proxy res.render() to add some magic
function controllerAction(name, plural, action, fn) { return function(req, res, next){ var render = res.render, path = __dirname + '/views/' + name + '/' + action + '.ejs'; res.render = function(obj, options, fn){ res.render = render; options = options || {}; options.locals = options.locals || {}; // Expose obj as the "users" or "user" local if (action == 'index') { options.locals[plural] = obj; } else { options.locals[name] = obj; } return res.render(path, options, fn); }; fn.apply(this, arguments); };}No newline at end of fileNo newline at end of file