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