Fixed 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 98e1823 · Browse files · 7 files·+173 added−176 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+173 added−176 removed
- other:+173 added−176 removed
Provenance
- Commit
6f2120b- Tree
6f9f206- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
98e1823
7 files changed+173 added−176 removed
modified:examples/mvc/controllers/app.js+6 added-6 removed
123456783456789
module.exports = { // / index: function(req, res){ res.render(); } // / index: function(req, res){ res.render(); }};No newline at end of fileNo newline at end of filemodified:examples/mvc/controllers/user.js+42 added-42 removed
123456567891011121314101112131415161718192021222318192021222324252526272829303132333435363738394041424344454647484950515253542728293031323334353637383940414243444546474849505152535455
// Fake user database for example
var users = [ { id: 0, name: 'TJ', email: 'tj@vision-media.ca' }, { id: 1, name: 'Simon', email: 'simon@vision-media.ca' } { id: 0, name: 'TJ', email: 'tj@vision-media.ca' } , { id: 1, name: 'Simon', email: 'simon@vision-media.ca' }];
function get(id, fn) { if (users[id]) { fn(null, users[id]); } else { fn(new Error('User ' + id + ' does not exist')); } if (users[id]) { fn(null, users[id]); } else { fn(new Error('User ' + id + ' does not exist')); }}
module.exports = { // /users index: function(req, res){ res.render(users); }, // /users index: function(req, res){ res.render(users); },
// /users/:id // /users/:id
show: function(req, res, next){ get(req.params.id, function(err, user){ if (err) return next(err); res.render(user); }); }, // /users/:id/edit edit: function(req, res, next){ get(req.params.id, function(err, user){ if (err) return next(err); res.render(user); }); }, // PUT /users/:id update: function(req, res, next){ var id = req.params.id; get(id, function(err){ if (err) return next(err); var user = users[id] = req.body.user; user.id = id; req.flash('info', 'Successfully updated _' + user.name + '_.'); res.redirect('back'); }); } show: function(req, res, next){ get(req.params.id, function(err, user){ if (err) return next(err); res.render(user); }); }, // /users/:id/edit edit: function(req, res, next){ get(req.params.id, function(err, user){ if (err) return next(err); res.render(user); }); }, // PUT /users/:id update: function(req, res, next){ var id = req.params.id; get(id, function(err){ if (err) return next(err); var user = users[id] = req.body.user; user.id = id; req.flash('info', 'Successfully updated _' + user.name + '_.'); res.redirect('back'); }); }};No newline at end of fileNo newline at end of filedeleted:examples/mvc/views/partials/messages.html-7 removed
1234567<% if (hasMessages) { %> <ul id="messages"> <% messages().forEach(function(msg){ %> <li><%- msg %></li> <% }) %> </ul><% } %>No newline at end of filemodified:examples/mvc/views/user/index.html+3 added-3 removed
1234564567<h1>Users</h1><%- partial('messages') %><ul> <% users.forEach(function(user){ %> <li><a href="/users/<%= user.id %>"><%= user.name %></a></li> <% }) %> <% users.forEach(function(user){ %> <li><a href="/users/<%= user.id %>"><%= user.name %></a></li> <% }) %></ul>No newline at end of fileNo newline at end of fileadded:examples/mvc/views/_messages.html+7 added
1234567<% if (hasMessages) { %> <ul id="messages"> <% messages().forEach(function(msg){ %> <li><%- msg %></li> <% }) %> </ul><% } %>No newline at end of filemodified:examples/mvc/views/layout.html+7 added-7 removed
1234567823456789<html> <head> <title>Express - MVC Example</title> <link rel="stylesheet" href="/style.css" /> </head> <body> <%- body %> </body> <head> <title>Express - MVC Example</title> <link rel="stylesheet" href="/style.css" /> </head> <body> <%- body %> </body></html>No newline at end of fileNo newline at end of filemodified:examples/mvc/mvc.js+108 added-111 removed
1 unmodified line2 /**3 * Module dependencies.4 */5 6-var fs = require('fs'),7- express = require('../../lib/express');6+var fs = require('fs')7+ , express = require('../../lib/express');8 9 exports.boot = function(app){10- bootApplication(app);11- bootControllers(app);10+ bootApplication(app);11+ bootControllers(app);12 };13 14 // App settings and middleware15 16 function bootApplication(app) {17- app.use(express.logger({ format: ':method :url :status' }));18- app.use(express.bodyDecoder());19- app.use(express.methodOverride());20- app.use(express.cookieDecoder());21- app.use(express.session());22- app.use(app.router);23- app.use(express.staticProvider(__dirname + '/public'));17+ app.use(express.logger({ format: ':method :url :status' }));18+ app.use(express.bodyDecoder());19+ app.use(express.methodOverride());20+ app.use(express.cookieDecoder());21+ app.use(express.session());22+ app.use(app.router);23+ app.use(express.staticProvider(__dirname + '/public'));24 25- // Example 500 page26- app.error(function(err, req, res){27- console.dir(err)28- res.render('500');29- });25+ // Example 500 page26+ app.error(function(err, req, res){27+ console.dir(err)28+ res.render('500');29+ });30 31- // Example 404 page via simple Connect middleware32- app.use(function(req, res){33- res.render('404');34- });31+ // Example 404 page via simple Connect middleware32+ app.use(function(req, res){33+ res.render('404');34+ });35 36- // Setup ejs views as default, with .html as the extension37- app.set('views', __dirname + '/views');38- app.register('.html', require('ejs'));39- app.set('view engine', 'html');36+ // Setup ejs views as default, with .html as the extension37+ app.set('views', __dirname + '/views');38+ app.register('.html', require('ejs'));39+ app.set('view engine', 'html');40 41- // Some dynamic view helpers42- app.dynamicHelpers({43- request: function(req){44- return req;45- },41+ // Some dynamic view helpers42+ app.dynamicHelpers({43+ request: function(req){44+ return req;45+ },46 47- hasMessages: function(req){48- return Object.keys(req.session.flash || {}).length;49- },47+ hasMessages: function(req){48+ return Object.keys(req.session.flash || {}).length;49+ },50 51- messages: function(req){52- return function(){53- var msgs = req.flash();54- return Object.keys(msgs).reduce(function(arr, type){55- return arr.concat(msgs[type]);56- }, []);57- }58- }59- });51+ messages: function(req){52+ return function(){53+ var msgs = req.flash();54+ return Object.keys(msgs).reduce(function(arr, type){55+ return arr.concat(msgs[type]);56+ }, []);57+ }58+ }59+ });60 }61 62 // Bootstrap controllers63 64 function bootControllers(app) {65- fs.readdir(__dirname + '/controllers', function(err, files){66- if (err) throw err;67- files.forEach(function(file){68- bootController(app, file);69- });65+ fs.readdir(__dirname + '/controllers', function(err, files){66+ if (err) throw err;67+ files.forEach(function(file){68+ bootController(app, file);69 });70+ });71 }72 73 // Example (simplistic) controller support74 75 function bootController(app, file) {76- var name = file.replace('.js', ''),77- actions = require('./controllers/' + name),78- plural = name + 's', // realistically we would use an inflection lib79- prefix = '/' + plural; 76+ var name = file.replace('.js', '')77+ , actions = require('./controllers/' + name)78+ , plural = name + 's' // realistically we would use an inflection lib79+ , prefix = '/' + plural; 80 81- // Special case for "app"82- if (name == 'app') {83- prefix = '/';84- }81+ // Special case for "app"82+ if (name == 'app') prefix = '/';83 86- Object.keys(actions).map(function(action){87- var fn = controllerAction(name, plural, action, actions[action]);88- switch(action) {89- case 'index':90- app.get(prefix, fn);91- break;92- case 'show':93- app.get(prefix + '/:id.:format?', fn);94- break;95- case 'add':96- app.get(prefix + '/:id/add', fn);97- break;98- case 'create':99- app.post(prefix + '/:id', fn);100- break;101- case 'edit':102- app.get(prefix + '/:id/edit', fn);103- break;104- case 'update':105- app.put(prefix + '/:id', fn);106- break;107- case 'destroy':108- app.del(prefix + '/:id', fn);109- break;110- }111- });84+ Object.keys(actions).map(function(action){85+ var fn = controllerAction(name, plural, action, actions[action]);86+ switch(action) {87+ case 'index':88+ app.get(prefix, fn);89+ break;90+ case 'show':91+ app.get(prefix + '/:id.:format?', fn);92+ break;93+ case 'add':94+ app.get(prefix + '/:id/add', fn);95+ break;96+ case 'create':97+ app.post(prefix + '/:id', fn);98+ break;99+ case 'edit':100+ app.get(prefix + '/:id/edit', fn);101+ break;102+ case 'update':103+ app.put(prefix + '/:id', fn);104+ break;105+ case 'destroy':106+ app.del(prefix + '/:id', fn);107+ break;108+ }109+ });110 }111 112 // Proxy res.render() to add some magic113 114 function controllerAction(name, plural, action, fn) {117- return function(req, res, next){118- var render = res.render,119- format = req.params.format,120- path = __dirname + '/views/' + name + '/' + action + '.html';121- res.render = function(obj, options, fn){122- res.render = render;123- // Template path124- if (typeof obj === 'string') {125- return res.render(obj, options, fn);126- }115+ return function(req, res, next){116+ var render = res.render117+ , format = req.params.format118+ , path = __dirname + '/views/' + name + '/' + action + '.html';119+ res.render = function(obj, options, fn){120+ res.render = render;121+ // Template path122+ if (typeof obj === 'string') {123+ return res.render(obj, options, fn);124+ }125 128- // Format support129- if (action == 'show' && format) {130- if (format === 'json') {131- return res.send(obj);132- } else {133- throw new Error('unsupported format "' + format + '"');134- }135- }126+ // Format support127+ if (action == 'show' && format) {128+ if (format === 'json') {129+ return res.send(obj);130+ } else {131+ throw new Error('unsupported format "' + format + '"');132+ }133+ }134 137- // Render template138- res.render = render;139- options = options || {};140- options.locals = options.locals || {};141- // Expose obj as the "users" or "user" local142- if (action == 'index') {143- options.locals[plural] = obj;144- } else {145- options.locals[name] = obj;146- }147- return res.render(path, options, fn);148- };149- fn.apply(this, arguments);135+ // Render template136+ res.render = render;137+ options = options || {};138+ // Expose obj as the "users" or "user" local139+ if (action == 'index') {140+ options[plural] = obj;141+ } else {142+ options[name] = obj;143+ }144+ return res.render(path, options, fn);145 };146+ fn.apply(this, arguments);147+ };148 }