My Web Log Book

Implementing an EventBus in MooTools

An event bus can be useful in making your web applications more maintainable and manageable. It does so by decoupling the source of the application events from their destination. In complex web applications, a change in one part of the screen might require other parts of the screen to update as a result.

A big part of this manageability comes with using custom events that are closer to our domain model than ‘clicks’, or ‘mouseovers’. If you have any doubts about the usefulness of custom events, you might want to read this article first.

A very common example of such a custom event is user registration. Once a user registers on a site, and assuming they are automatically logged-in right after, then various parts on the screen will need to be updated as a result. A registration successful message pops-up somewhere. The navigation panel is updated with a Logout link. Other things that only registered users can do will get activated.

If your application is highly modularized into widgets, views, etc., then you might not want to create an unnecessary binding between those components just because they need to listen to a particular event that some other component happens to fire. All that a widget needs to know is that an event of interest has fired, regardless of the source (for most cases), and that it must respond to it. Ray Ran gives a nice example with a full walkthrough in his talk at Google I/O and explains how an EventBus solves the problem. I highly recommend watching the talk since the problem and solution that he demonstrates is very much applicable to any complex web application and not just apps written in Google Web Toolkit. An event bus acts as a central broker for communicating events to various parts of the application that need not know about each other’s presence. It basically publish-subscribe and somewhat similar to the NSNotificationCenter in Cocoa if you’re familiar with that.

Once you are thoroughly convinced that you absolutely need this, you would really like the solution too if you’re using MooTools. Why? checkout for yourself -

var EventBus = new Class({Implements: Events});

1 line. That’s all that you need to start using an EventBus. MooTools classes can already handle events by implementing Events in Class.Extras. All that is need is creating an object of a class that does this and passing it around to whoever needs is using dependency injection and each object can register and post their events to this event bus.

Here’s a small example that makes use of this event bus with a couple of different types of classes:

Here’s a tiny wrapper around the EventBus class above. All it does is change the names addEvent, removeEvent, and fireEvent to be a little more semantic with observers. Nothing fancy.
  1. vombat posted this