Fixed flash example. Closes #392

This commit is contained in:
Tj Holowaychuk 2010-08-09 07:19:55 -07:00
parent 498115d5d9
commit ae1c20fad9
2 changed files with 25 additions and 18 deletions

View File

@ -26,23 +26,30 @@ app.set('view engine', 'ejs');
app.dynamicHelpers({
messages: function(req, res){
// Grab the flash messages
var messages = req.flash();
// We will render the "messages.ejs" partial
return res.partial('messages', {
// Our target object is our messages
object: messages,
// We want it to be named "types" in the partial
// since they are keyed like this:
// { info: ['foo'], error: ['bar']}
as: 'types',
// Pass a local named "hasMessages" so we can easily
// check if we have any messages at all
locals: { hasMessages: Object.keys(messages).length },
// We dont want dynamicHelpers in this partial, as
// it would cause infinite recursion
dynamicHelpers: false
});
// In the case of flash messages
// we return a function, allowing
// flash messages to only be flushed
// when called, otherwise every request
// will flush flash messages regardless.
return function(){
// Grab the flash messages
var messages = req.flash();
// We will render the "messages.ejs" partial
return res.partial('messages', {
// Our target object is our messages
object: messages,
// We want it to be named "types" in the partial
// since they are keyed like this:
// { info: ['foo'], error: ['bar']}
as: 'types',
// Pass a local named "hasMessages" so we can easily
// check if we have any messages at all
locals: { hasMessages: Object.keys(messages).length },
// We dont want dynamicHelpers in this partial, as
// it would cause infinite recursion
dynamicHelpers: false
});
}
}
});

View File

@ -1,2 +1,2 @@
<h1>Flash Message Example</h1>
<%- messages %>
<%- messages() %>