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;
});
@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