简体   繁体   中英

Can someone explain what the Backbone dispatcher does?

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 =/

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM