chovy
chovy

Reputation: 75686

Flash a message with JQuery, similar to rails flash or connect-flash in node

I am flashing messages to the client from the server using connect-flash in my node.js app. Similar to how you can flash messages in Rails apps.

I need to have this same sort of dynamic message flashing but on the client side using jQuery.

I think the only way is to save a message in a cookie, then redirect and on page load read from the cookie to add the message to the page and delete the cookie.

Is there a plugin that will do this?

Some requirements are that I can flash info and error messages.

Upvotes: 2

Views: 1012

Answers (1)

max
max

Reputation: 102036

You could just add a listener to the ajax calls. Using cookies will be somewhat messy and is only needed when you need persistence across page loads.

<script>
    var flashHandler = $('#flashes');

    flashHandler.on('flash', function(event, message){
        var flash = $('<div class="flash">');
        flash.text(message);
        flash.on('click', function(){
            $(this).remove();
            });
        $(this).append(flash);
    });

    $.ajax('/test',{
        'success' : function(){
            flashHandler.trigger('flash', ['Eureka!']);
        }
    });
</script>

Upvotes: 1

Related Questions