Added flash messages to blog 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 e7c442d · Browse files · 7 files·+66 added−7 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+66 added−7 removed
- other:+66 added−7 removed
Provenance
- Commit
ec0982c- Tree
06988e6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
e7c442d
7 files changed+66 added−7 removed
modified:examples/blog/models/post.js+11 added
14 unmodified lines1516171819202122232425262728293031323334 unmodified lines14 unmodified lines db[this.id] = this; fn();};
Post.prototype.validate = function(fn){ if (!this.title) return fn(new Error('_title_ required')); if (!this.body) return fn(new Error('_body_ required')); if (this.body.length < 10) { return fn(new Error( '_body_ should be at least **10** characters long, was only _' + this.title.length + '_')); } fn();};
Post.prototype.update = function(data, fn){ this.updatedAt = new Date; for (var key in data) { if (undefined != data[key]) {34 unmodified linesmodified:examples/blog/public/style.css+18 added
35 unmodified lines363738394041424344454647484950515253545556575835 unmodified linesa.edit { margin-left: 10px; font-size: 11px; font-weight: normal;}.date { font-size: 11px;}#messages ul { padding: 10px; border: 1px solid;}#messages ul li { list-style: none;}#messages ul.info { color: #2EBBE6; background: #F7FBFD;}#messages ul.error { color: #E4250C; background: #FDEAE7;}No newline at end of fileNo newline at end of filemodified:examples/blog/routes/post.js+21 added-6 removed
20 unmodified lines2122232425252627282912 unmodified lines424344454647464748495051525354555657585917 unmodified lines7778798073747581828384858687888990919220 unmodified lines
app.param('post', function(req, res, next, id){ Post.get(id, function(err, post){ if (err) return next(err); if (!post) return next(new Error('failed to post user ' + id)); if (!post) return next(new Error('failed to load post ' + id)); req.post = post; next(); });});12 unmodified lines
app.post('/post', function(req, res){ var data = req.body.post , post = new Post(data.title, data.body); post.save(function(err){ res.redirect('/post/' + post.id); post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); } post.save(function(err){ req.flash('info', 'Successfully created post _%s_', post.title); res.redirect('/post/' + post.id); }); });});
/**17 unmodified lines */
app.put('/post/:post', function(req, res, next){ var post = req.post; post.update(req.body.post, function(err){ if (err) return next(err); res.redirect('back'); post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); } post.update(req.body.post, function(err){ if (err) return next(err); req.flash('info', 'Successfully updated post'); res.redirect('back'); }); });});No newline at end of fileNo newline at end of filemodified:examples/blog/views/post/form.jade+2 added
2 unmodified lines3 h1 Editing #{post.title}4 - else5 h1 New Post6 7+!= messages()8+9 form#post(action='/post' + (post.title ? '/' + post.id : ''), method='post')10 - if (post.title)11 input(type='hidden', name='_method', value='put')12 p8 unmodified linesmodified:examples/blog/views/post/index.jade+3 added
2 unmodified lines3 h24 = post.title5 a.edit(href='/post/' + post.id + '/edit') Edit6 7+ // flash messages8+ != messages()9+ 10 // dates11 p.date.created Created at #{post.createdAt}12 - if (post.updatedAt)13 p.date.updated Updated at #{post.updatedAt}3 unmodified linesmodified:examples/blog/views/index.jade+2 added
1 h1 Blog2 3+!= messages()4+5 - if (count)6 p Display all #{count} post(s)7 #posts!= partial('post', posts)8 - else11 unmodified linesmodified:examples/blog/app.js+9 added-1 removed
4 unmodified lines5 /**6 * Module dependencies.7 */8 9-var express = require('../../lib/express');9+var express = require('../../lib/express')10+ , messages = require('express-contrib/messages');11 12 app = express.createServer();13 14 // Config15 16 app.set('views', __dirname + '/views');17 app.set('view engine', 'jade');18 19+// Flash message helper provided by express-contrib20+// $ npm install express-contrib21+22+app.dynamicHelpers({ messages: messages });23+24 // Middleware25 26 app.configure(function(){27 app.use(express.logger('\x1b[33m:method\x1b[0m \x1b[32m:url\x1b[0m :response-time'));28 app.use(express.bodyDecoder());29 app.use(express.methodOverride());30+ app.use(express.cookieDecoder());31+ app.use(express.session());32 app.use(app.router);33 app.use(express.staticProvider(__dirname + '/public'));34 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));35 });8 unmodified lines