1

So I'm learning Backbone from scratch using the documentation.

Here is the snippet im trying to understand

var object = {};

_.extend(object, Backbone.Events);

object.on("alert", function(msg) {
  alert("Triggered " + msg);
});

object.trigger("alert", "an event");

For example, to make a handy event dispatcher that can coordinate events among different areas of your application: var dispatcher = _.clone(Backbone.Events)

The code above works like a charm but I don't get the dispatcher part, what does it do? And how do I use it, already googled but the examples were too advanced for me =/

Gabriel ThaKid
  • 857
  • 3
  • 9
  • 19
  • possible duplicate of [backbone.js - working with the event dispatcher var dispatcher = \_.clone(Backbone.Events)](http://stackoverflow.com/questions/9176416/backbone-js-working-with-the-event-dispatcher-var-dispatcher-clonebackbon) – Puigcerber Feb 24 '14 at 09:52
  • I already saw that answer, but the example given is too advanced sorry – Gabriel ThaKid Feb 24 '14 at 09:56
  • You might want to check out the *Event Aggregator* section of [this article](http://lostechies.com/derickbailey/2013/03/18/event-aggregator-andorvs-mediator-a-tale-of-two-patterns/) – Jonathan Beebe Feb 24 '14 at 15:44

1 Answers1

0

This is one of several possible ways to decouple views. Decouple mean is to make one view know nothing about another one. Decoupling makes parts of your application independent of each other. This is good for debugging, reusability and so on.

Example you provided is not complete and it seems the reason why you don't understand what does it do and what is the purpose.

Good detailed article for this issue: http://blog.safaribooksonline.com/2013/10/02/decoupling-backbone-applications-with-pubsub/

zelibobla
  • 1,498
  • 1
  • 16
  • 23