Search code examples
actionscript-3apache-flexeventsdesign-patternsair

Global Event Dispatcher


What would be the dissadvantages to using a Global Event Dispatcher, as described here: http://www.riaspace.com/2010/11/unframework-so-how-to-getaway-without-any-framework/ Bearing in mind, I am working solely on small(ish) applications.

// eventDispatcher.as script file

package 
{
    import flash.events.IEventDispatcher;

    public function get eventDispatcher():IEventDispatcher
    {
        return _eventDispatcher;
    }
}

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

var _eventDispatcher:IEventDispatcher = new EventDispatcher();

public class MyViewPM
{
    public function btn_clickHandler():void
    {
        eventDispatcher.dispatchEvent(new MyEvent(MyEvent.EVENT_TYPE));
    }
}

Solution

  • If you are working on smaller projects and on your own, then a global dispatcher is probably fine and can save you a bit of time.

    The problem is with bigger applications and several developers working on the same codebase. My experience is that (less experienced) developers will quickly abuse the global event system and send events over it that should really be local events (mostly view events). This makes the code hard to understand, test and modify since you have little control over who dispatches and who listens for certain events. As with global state, data can get modified in different places at the same time which can lead to debugging nightmares.

    If you are interested in an enhanced global dispatcher, check out the AS3Commons EventBus.