I think what you need is a global pub-sub facility. Modules you have subscribe to certain events. These subscriptions (functions) get executed when that event is published, usually with data.
This allows different modules, from different parts of your program, to interact even without direct visibility with each other. The only dependency visible to your modules is the pub-sub module.
For instance this:
// ModuleA.js
PubSub.module('moduleA',function(tower){
tower.subscribe('foo',function(data){
// executes when somewhere else publishes a foo
alert('foo executed in A with ' + data.join(' '));
});
});
// ModuleB.js
PubSub.module('moduleB',function(tower){
tower.subscribe('foo',function(data){
// executes when somewhere else publishes a foo
alert('foo executed in B with ' + data.join(' '));
});
});
// ModuleC.js
PubSub.module('moduleC',function(tower){
$(document).on('click',function(){
// publish a foo event, passing 1 and 2, which arrives as "data" in each handler
tower.publish('foo','1','2');
});
});
jQuery has built-in event-handling capabilities, which also allow custom events. You can use this ability as your pub-sub facility.
var test = $(document); // You can bind it to any object. document is handy.
test.on('sampleevent',function(){ // Add a handler
alert('executed');
});
test.trigger('sampleevent'); // Execute
You can also implement your own, which doubles as a good programming practice. Here's my version of it, which I based on Node.js's EventEmitter.