1

I am using a Backbone.js app with a central dispatcher and central view called AppView.

In an initializer, I declare the central dispatcher with:

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

This dispatcher is passed to every view the app has. Each one can trigger and/or bind to custom events for this dispatcher. In this way, I pretend to allow communication between views without having references to nested views / etc.

My issue is:

If I have several views listening for the same event, when the x event is triggered by someone, all of those views can respond to it. My problem is: Depending of the context (flow) of the application, not all of the listeners should react to that event...

Any workaround? Am I doing something wrong from what a central dispatcher should be?

Thanks!

poseid
  • 6,986
  • 10
  • 48
  • 78
joTa
  • 23
  • 3
  • 2
    Maybe you need to differentiate the context by using different events. Or perhaps the views need to check the context when their event handlers get called. Hard to say without more specifics. – mu is too short May 28 '13 at 19:05
  • hi mu_is_too_short, I agree with your comment above. The context of events can easily be limited to view groupings, etc. One thing I am wondering lately, would controller objects help, as discussed in my question here: http://stackoverflow.com/questions/16769087/how-to-design-a-controller-in-backbone-js – poseid May 28 '13 at 20:07

2 Answers2

0

I think you may need "Mediator", It will broadcast event and your custom event to every views that subscribe it.

Hetfield Joe
  • 1,443
  • 5
  • 15
  • 26
  • Really good option, i wont use the class itself but it gave me a lot of ideas in how to implement this for myself (in my next project will definitely give a chance to Mediator.js). Thanks! – joTa May 29 '13 at 15:36
0

I recently face with a similar situation and looking for solutions I found differents solutions:

  1. Using a central event dispatcher which is shared by all the models and views. I get with this solutions in this interesting post:

http://www.michikono.com/2012/01/11/adding-a-centralized-event-dispatcher-on-backbone-js/

  1. Make the dispatcher natively accesible for all the models and views as it is developed in this post:

http://devlicio.us/blogs/mike_nichols/archive/2011/10/20/backbone-events-and-aggregator-update.aspx

From my experience, to maintain in good shape the code it is worth to avoid using central dispatchers. We create a set of dispatchers for every module of the app (each module can contain differents views that need to communicate each one):

var searchTabEvents = _.extend({}, Backbone.Events); //handler events for the search tab
var visualizationEvents = _.extend({}, Backbone.Events); //handler events for the visualization tab

And when we create the views we pass to the view the appropiatte event handler:

var extEvents = searchTabEvents;

var searchView = new SearchView({customEvents : extEvents});
ftrujillo
  • 1,172
  • 1
  • 16
  • 30
  • first link is dead here is an archive of it. https://web.archive.org/web/20150225004842/https://michikono.com/2012/01/11/adding-a-centralized-event-dispatcher-on-backbone-js/ – Jeff May 04 '20 at 02:22