0

I'm starting implementing Backbone in a web app.

The app has already some objects of its own, and also a Mediator (here called EventTools) for cross-object event communication. So for ex. an object subscribes like this:

  EventTools.add({

    "gt_pos_ready":function(coords){
      //code;
    }.bind(this),

  });

And then some other object fires the event:

EventTools.fire("gt_pos_ready", args..);

Fine. But what if I want a view or other Backbone object to listen to EventTools firing some event?

Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118

1 Answers1

1

You can use Backbone.Events instead of your own way to trigger and listen events:

_.extend(EventTools, Backbone.Events);

EventTools.on("gt_pos_ready", function(coords) {
  /*Code for gt_pos_ready()*/
});

EventTools.trigger("gt_pos_ready", {LNG:"40.542343",LAT:"32.232423"});

I'm sure you are using lodash/underscore because it's a Backbone dep, so this should work.

fcortes
  • 1,338
  • 3
  • 11
  • 26
  • That's interesting. So I would not use the original implementation of EventTools (it's a class I made using $.Callbacks functionalities). [link](http://jsfiddle.net/stratboy/aYb5B/) So, then, what should be the nature of EventTools in this case? – Luca Reghellin Nov 27 '13 at 15:34
  • In the end, I realized (as you wrote) that Backbone can act as a Mediator itself. I could simply use Backbone alone. Only for organization purposes, I extended just an empty object an saved to EventTools `var EventTools = _.extend({},Backbone.Events);` For anyone interested in a simple mediator class that uses $.Callbacks, the link in my previous comment is just good. Thank you @fcortes – Luca Reghellin Dec 05 '13 at 10:27