diff --git a/examples/flash/app.js b/examples/flash/app.js index 1bbc2f40..46cd0f18 100644 --- a/examples/flash/app.js +++ b/examples/flash/app.js @@ -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 + }); + } } }); diff --git a/examples/flash/views/index.ejs b/examples/flash/views/index.ejs index b5a303de..89fff579 100644 --- a/examples/flash/views/index.ejs +++ b/examples/flash/views/index.ejs @@ -1,2 +1,2 @@