Skip to content

Instantly share code, notes, and snippets.

@juanghurtado
Last active December 13, 2015 22:19
Show Gist options
  • Save juanghurtado/4984105 to your computer and use it in GitHub Desktop.
Save juanghurtado/4984105 to your computer and use it in GitHub Desktop.
Which option is better? A global CommunicationBus object (with Wreqr), or each module with their owns Wreqr instances?
// 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;
});
@mxriverlynn
Copy link

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.

@juanghurtado
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment