README.md in modularity-rails-0.17.0 vs README.md in modularity-rails-0.19.0

- old
+ new

@@ -53,9 +53,54 @@ Each module has a container. The container is the outermost DOM element of a section. Everything the module does must happen inside this container. The module is responsible for managing the inner DOM-structure of the container. +## Events + +Modules are supposed to form a loosely coupled architecture, in which they communicate with each other through events. +Similar to jQuery events, a module can trigger an event using its `trigger` method, like so: +```coffeescript +@trigger 'user_selected', user +``` + +The first argument is the event name, the second one an optional data payload. The event is a normal jQuery event, +triggered from the container of the module. + +Other modules can subscribe to events from particular modules using a similar mechanism: +```coffeescript +@user_list.on 'user_selected', (e, user) -> ... +``` + +The first argument is the jQuery event object, the second one the data payload. + +### Event Autobinding + +If you want to avoid writing lots of boilerplate code to wire up events, use Modularities `autobind` method together with the convention to name your event handler methods "on_#{module_name}_#{event_name}". +For example, to listen to the "entry_created" event of a module called "userlist", name your method "on_userlist_entry_created". To illustrate this more, the following two pieces of code are equivalent: + +Manual event listener setup: +```coffeescript +@userlist = new UserList() +@userlist.on 'entry_selected', @user_selected +@userlist.on 'entry_created', @user_created +@userlist.on 'entry_deleted', @user_deleted + +user_selected: (e, user) -> ... +user_created: (e, user) -> ... +user_deleted: (e, user) -> ... +``` + +Automatic event listener setup: +```coffeescript +@userlist = new UserList() +@autobind() + +on_userlist_entry_selected: (e, user) -> ... +on_userlist_entry_created: (e, user) -> ... +on_userlist_entry_deleted: (e, user) -> ... +``` + ## Mixins Similar to Ruby mixins, mixins in Modularity allow to include orthogonal functional aspects defined in separate objects into a class.