This forum has been very useful to me, so I thought I’d share some code and return the favor.
Do you ever find yourself staring at a class asking “What events does this thing raise?” The only way to find out is to look for const strings or look in the documentation. Even worse are interfaces that extend IEventDispatcher. You have to go straight to the documentation to discover the valid events.
The solution to this problem is to use an “EventObject.” They’re a very basic class encapsulation of IEventDispatcher and the event string. They’re completely backwards compatible with the standard IEventDispatcher model but they serve to document and explicitly call out which events an interface or class can raise.
Here’s the event object code:
package HiddenCity.System
{
import flash.events.*;
public class EventObject
{
protected var _eventDispatcher:IEventDispatcher;
protected var _eventType:String;
public function EventObject(eventDispatcher:IEventDispatcher, eventType:String)
{
this._eventDispatcher = eventDispatcher;
this._eventType = eventType;
}
public function get eventType():String
{
return this._eventType;
}
public function addEventListener(handler:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
this._eventDispatcher.addEventListener(this._eventType, handler, useCapture, priority, useWeakReference);
}
public function removeEventListener(handler:Function, useCapture:Boolean = false):void
{
this._eventDispatcher.removeEventListener(this._eventType, handler, useCapture);
}
public function dispatchEvent(event:Event = null):void
{
if (event == null) //Use a default event.
{
event = new Event(this.eventType);
}
if (event.type != this._eventType)
{
throw new Error("Parameter \"event\" must have type: " + this._eventType);
}
this._eventDispatcher.dispatchEvent(event);
}
public function get willTrigger():Boolean
{
return this._eventDispatcher.willTrigger(this._eventType);
}
public function get hasListener():Boolean
{
return this._eventDispatcher.hasEventListener(this._eventType);
}
}
}
Here’s a sample class that raises a “COMPLETED” event:
package
{
import flash.events.*;
import HiddenCity.System.*;
public class EventRaiser extends EventDispatcher
{
private var _completedEventObject:EventObject;
public function get completedEventObject():EventObject
{
return this._completedEventObject;
}
public function EventRaiser()
{
this._completedEventObject = new EventObject(this, "COMPLETED");
}
public function doSomethingToRaiseEvent()
{
this._completedEventObject.dispatchEvent();
}
}
}
And listening for events is easy too:
var eventRaiser:EventRaiser = new EventRaiser();
eventRaiser.completedEventObject.addEventListener(completedHandler);
eventRaiser.doSomethingToRaiseEvent();
eventRaiser.completedEventObject.removeEventListener(completedHandler);
Notice that when both raising the event and listening to the event, you never have to specify the event string! (If you do need the event string for whatever reason, you can get it from EventObject.eventType.)
Yes, the setup is a little more expensive than just using EventDispatcher, but it makes for simpler, self documenting code and makes interfaces that raise events much more explicit. And the code builds on and coexists with Flash’s standard IEventDispatcher model.
Hope this helps someone.