Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success & Error Flashes #20

Open
Scribblets opened this issue May 27, 2015 · 2 comments
Open

Success & Error Flashes #20

Scribblets opened this issue May 27, 2015 · 2 comments

Comments

@Scribblets
Copy link

On a particular page, I want to have a flash for both success and error messages. However, I cannot figure out how to determine which is which in the view template:

request.flash('success', message);
request.flash('error', message);

How to access the message in the view: <%= message %>

if(message.error) {
     // Show error state
} else if(message.success) {
     // Show success state
}

But I'm not seeing any way to do this? Is this possible?

@mrmanicou
Copy link

In your handler you specify the object to create that message variable that you send to the view. Like so:
...
response.render('view', {message:{success: request.flash('success'),error: request.flash('error')}});

@isimmons
Copy link

isimmons commented Jul 5, 2015

Makes for some extra work because in your view you have to check if the array is empty. Doing it the above suggested way, if you don't have any success type errors you will still get

console.log(messages);
//output
{success: [], error: ['some error message', 'another error message']

You can also call request.flash() without specifying the type and get the same output

response.render('view', { messages:req.flash() })

Just playing around with this and had to do the following to retrieve messages in a ejs template

<% if (typeof messages.error !== 'undefined' && messages.error.length > 0) { %>
    <p class="error"><%= messages.error %></p>
<% } %>
<% if (typeof messages.info !== 'undefined' && messages.info.length > 0) { %>
    <p class="info"><%= messages.info %></p>
<% } %>
<% if (typeof messages.success !== 'undefined' && messages.success.length > 0) { %>
    <p class="info"><%= messages.success %></p>
<% } %>

Gonna see if I can figure out a good helper function to create or just put this in it's own messages template to be included where ever I need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants