Skip to content

Commit

Permalink
Implement multiple flash messages support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wlzch committed Jul 24, 2012
1 parent 6ead60e commit c7e9389
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/express2/app.js
Expand Up @@ -32,6 +32,11 @@ app.get('/flash', function(req, res){
res.redirect('/');
});

app.get('/multiple-flash', function(req, res){
req.flash('info', ['Welcome', 'Please Enjoy']);
res.redirect('/');
});

app.get('/no-flash', function(req, res){
res.redirect('/');
});
Expand Down
5 changes: 5 additions & 0 deletions examples/express3/app.js
Expand Up @@ -34,4 +34,9 @@ app.get('/no-flash', function(req, res){
res.redirect('/');
});

app.get('/multiple-flash', function(req, res){
req.flash('info', ['Welcome', 'Please Enjoy']);
res.redirect('/');
});

app.listen(3000);
6 changes: 6 additions & 0 deletions lib/flash.js
Expand Up @@ -2,6 +2,7 @@
* Module dependencies.
*/
var format = require('util').format;
var isArray = require('util').isArray;


/**
Expand Down Expand Up @@ -63,6 +64,11 @@ function _flash(type, msg) {
if (arguments.length > 2 && format) {
var args = Array.prototype.slice.call(arguments, 1);
msg = format.apply(undefined, args);
} else if (isArray(msg)) {
msg.forEach(function(val){
(msgs[type] = msgs[type] || []).push(val);
});
return msgs;
}
return (msgs[type] = msgs[type] || []).push(msg);
} else if (type) {
Expand Down
7 changes: 7 additions & 0 deletions test/flash-test.js
Expand Up @@ -61,6 +61,13 @@ vows.describe('flash').addBatch({
assert.lengthOf(Object.keys(req.session.flash), 1);
assert.lengthOf(req.session.flash['info'], 2);
},
'should set flash messages in one call' : function(err, req, res) {
req.flash('warning', ['username required', 'password required']);
var msgs = req.flash('warning');
assert.lengthOf(msgs, 2);
assert.equal(msgs[0], 'username required');
assert.equal(msgs[1], 'password required');
},
'should get and clear multiple previously set flash messages' : function(err, req, res) {
var msgs = req.flash('info');
assert.lengthOf(msgs, 2);
Expand Down

0 comments on commit c7e9389

Please sign in to comment.