Flash Messages BackboneJS

Is there an extension for the basics for Flash messages? This seems to be a common feature in the Web Framework (at least on the server side). There seems to be no one, and I tried to make my own:

class FlashMessenger extends Backbone.Model constructor: -> @messages = [] # add a message to the messages array add: (type, message) -> @messages.push type: type message: message # returns all existing messages and clearing all messages getMessages: -> ret = @messages.slice(0) @messages = [] return ret 

Now I was wondering how I can automatically enter them into my views. I will like my posts when I use Backbone.Router.navigate() for example:

 app.flashMessages.add("success", "Successfully logged in") appRouter.navigate("dashboard") # flash messages should show when I render the view 
+8
source share
1 answer

My 5 cents - it seems a bit overkill to use Backbone for flash messages. If the page has only 1 instance of the flash message, you better not use a separate model for it.

Instead, I would use the view for the Flash message and the global dispatcher:

 Dispatcher = _.extend({}, Backbone.Events); 

Create view:

 var FlashMessage = Backbone.View.extend({ initialize: function() { Dispatcher.bind('show_flash_message', this.render); }, render: function(msg) { // do something with the message } }); 

And from the side of the application where you need to show the flash message, do

 Dispatcher.trigger('show_flash_message', 'Some message'); 
+13
source share

All Articles