Last active
December 13, 2015 22:19
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Global Event Bus | |
define(['backbone.wreqr'], function(Wreqr) { | |
var CommunicationBus = { | |
commands : new Wreqr.Commands(), | |
reqres : new Wreqr.RequestResponse(), | |
vent : new Wreqr.EventAggregator() | |
}; | |
return CommunicationBus; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.