fixed flash 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 1ac18f7 · Browse files · 4 files·+54 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+54 added−54 removed
- other:+54 added−54 removed
Provenance
- Commit
322300c- Tree
0513063- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
1ac18f7
4 files changed+54 added−54 removed
deleted:examples/flash/views/partials/messages.ejs-12 removed
1-<% if (hasMessages) { %>2- <div id="messages">3- <% for (var type in types) { %>4- <h2><%= type %> messages</h2>5- <ul id="<%= type %>">6- <% types[type].forEach(function(msg){ %>7- <li><%= msg %></li>8- <% }) %>9- </ul>10- <% } %>11- </div>12-<% } %>added:examples/flash/views/_messages.ejs+12 added
1+<% if (hasMessages) { %>2+ <div id="messages">3+ <% for (var type in types) { %>4+ <h2><%= type %> messages</h2>5+ <ul id="<%= type %>">6+ <% types[type].forEach(function(msg){ %>7+ <li><%= msg %></li>8+ <% }) %>9+ </ul>10+ <% } %>11+ </div>12+<% } %>modified:examples/flash/views/layout.ejs+3 added-3 removed
1 <html>2- <body>3- <%- body %>4- </body>2+ <body>3+ <%- body %>4+ </body>5 </html>modified:examples/flash/app.js+39 added-39 removed
9 unmodified lines1011121314151415161718197 unmodified lines27282930313233343536373839404142434445464748495051525354553132333435363738394041424344454647484950515253545556575859606162636465666061626364656667686970717273747071727374757677789 unmodified lines
// App with session support
var app = express.createServer( express.cookieDecoder(), express.session() express.cookieDecoder() , express.session());
// View settings
7 unmodified lines// template, "messages" will be populated with the return// value of this function.
app.dynamicHelpers({ messages: function(req, res){ // 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 }); } messages: function(req, res){ // 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 }); } }});
app.dynamicHelpers({ // Another dynamic helper example. Since dynamic // helpers resolve at view rendering time, we can // "inject" the "page" local variable per request // providing us with the request url. page: function(req, res){ return req.url; } // Another dynamic helper example. Since dynamic // helpers resolve at view rendering time, we can // "inject" the "page" local variable per request // providing us with the request url. page: function(req, res){ return req.url; } });
app.get('/', function(req, res){ // Not very realistic notifications but illustrates usage req.flash('info', 'email queued'); req.flash('info', 'email sent'); req.flash('error', 'delivery failed'); res.render('index'); // Not very realistic notifications but illustrates usage req.flash('info', 'email queued'); req.flash('info', 'email sent'); req.flash('error', 'delivery failed'); res.render('index');});
app.listen(3000);console.log('Express app started on port 3000');No newline at end of fileNo newline at end of file