-
-
Save juanghurtado/4984105 to your computer and use it in GitHub Desktop.
// Global Event Bus | |
define(['backbone.wreqr'], function(Wreqr) { | |
var CommunicationBus = { | |
commands : new Wreqr.Commands(), | |
reqres : new Wreqr.RequestResponse(), | |
vent : new Wreqr.EventAggregator() | |
}; | |
return CommunicationBus; | |
}); |
// OPTION 1 | |
// Each module has their own "commands", "reqres" and "vent" objects, | |
// instancing new Wreqr objects for each one | |
define(['backbone.wreqr'], function(Wreqr) { | |
var SampleModule = { | |
commands : new Wreqr.Commands(), | |
reqres : new Wreqr.RequestResponse(), | |
vent : new Wreqr.EventAggregator() | |
}; | |
SampleModule.commands.addHandler("commandName", function() { | |
console.log('executed "commandName"!'); | |
}); | |
return SampleModule; | |
}); |
// OPTION 2 | |
// Each module has access to a global "CommunicationBus" object, | |
// which returns an instance of every Wreqr object | |
define(['modules/communication-bus'], function(CommunicationBus) { | |
var SampleModule = {}; | |
CommunicationBus.commands.addHandler("sampleModule:commandName", function() { | |
console.log('executed "sampleModule:commandName"!'); | |
}); | |
return SampleModule; | |
}); |
It doesn't matter if you have 1 or 100 different buses to use, though. The correct use of them is dependent on the context and what needs to be aware of the messages.
Try it one way and see if you like it. If you don't like it, take the time to think about why. What about this context and specific need makes this use of this bus incorrect? In what context would this use be correct?
The largest mistake that we can make is thinking that if I do it one way, I have to stick with that forever. Don't be afraid of doing it wrong. Be afraid of not fixing it when you realize it's wrong.
Thanks for your time, Derick. It's amazing how you care about your community :)
I'll stick with an app-wide CommunicationBus object. I made the changes and everything seems right, it feels good. Maybe later on, with more modules on the app or whatever, I'll change my mind. But right now, it fits like that.
Thank you again.
In this case there is no "local bus". It's a module public bus. It is intended to be used from inside and outside the module. That's why I'm not sure if, in this case, it's better a system-wide bus.