I’m trying to build an application in ExtJs/Javascript and noticed events are good way to keep component independent to each others. However, the way events are raised and captured makes them a bit more specific to caller and receiver.
Is there any better way to do this. I’m planning to write one center EventBus class using which any component can raise one event and another can register to listen to it.
Is there any library already available to achieve something like this. I saw backbone.js not sure it’s doing same thing what i’m looking.
Writing a
Mediatoris dead-simple, if you’ve already got a framework that does event-delegation, using apublisher/subscriberorobservermodel (whether they’re calledeventsoractionsor they call the system anemitteror whatever).You just make the
Mediatorthe onlyemitter/publisher/etc, and then give your modules/classes/services/etc access to thatMediator.If you’d like, you could even do something inside of your classes, to wrap your mediator, so that you can have public methods for your “class” / module, which will look like their own events, but will be passing data through to the mediator:
Your modules now all have access to one single mediator or however many mediators you choose… …you could give each widget its own mediator, which its individual components could share… …plus give each widget access to a system-wide mediator, so while components can’t access global-services like AJAX or the DOM, the widget itself can.
And by providing an interface in your components, you could program neatly (in a more classical way in your main process, rather than in the lambda/callback-heavy JS way), using public wrapper calls, so now:
Or however something similar might look in your project —
imageLoaderknows nothing about any other modules, but when you call its.firemethod, any modules sharing the same mediator, and listening for that event will be notified.