wiki:FAQWhatAreListeners

What are listeners and what do they do in Mach-II?

Overview

If you are new to Mach-II, you may be confused about what purpose listeners serve. Listeners are the "connecting tissue" between Mach-II and your model by acting as a "proxy" or "gateway". They capture events that are defined in your configuration file, interact with your model and pass data back to the framework.

Three Primary Functions of Listeners

They:

  • are notified of events they have registered interest in
  • perform Mach-II specific logic that should not be located in your model such as getting and setting data to and from the event object and talk to your model
  • can announce new events if needed

Listeners should perform Mach-II specific logic such as announcing new events or getting Mach-II properties. If your model contains Mach-II specific logic such as reference to the event object, you should consider refactoring that logic out of your model and into your listener. It may appear that listeners are a good place for model logic, but when you architect your application in this manner you introduce very tight coupling between Mach-II and your model. Your model should have no knowledge about the framework or dependencies on framework related logic. Be aware that reusing your model for Flex or Flash based UI becomes nearly impossible to accomplish since they will not make use of the Mach-II specific logic.

The notification of interest from events that a listener may have come from the <notify> and <publish> commands that are nested inside your event-handlers. The <notify> is more of a "direct" notification (somewhat less implicit, however this subject has been argument many times in the past) while the <publish> command overs a completely implicit notification because this command broadcasts a message in which one or more listener methods may have a registered interest in. A listener itself does not register interest in a specific event-handler. The registration of interest comes from the use of the <notify> and <publish> commands.

For more information on listeners, read the introduction to listeners tutorial.

Additional Information and Considerations

  • All listeners must extend the MachII.framework.Listener base component which is included with the core framework files.
  • All listeners Must be registered in the <listeners> section of your Mach-II configuration file.
  • Do not create an init() method in your listener as this overrides an inherited method. Doing this will break the framework. Use the configure() method for instantiation logic. The framework will call this method for you at the appropriate time.
  • Listeners can take a specialized CFC called an invoker. These are rarely required in most situations. Starting with Mach-II 1.1.0, the framework will automatically instantiate the default invoker (MachII.framework.invokers.EventArgsInvoker) for you. If you do not know what an invoker is, you probably don't need to know this.

Back to FAQs