Macromedia flex 2 Manual

Page of 254
Dispatching custom events
41
Once defined using the 
[Event]
 metadata tag, you can refer to the event in an MXML file, as 
the following example shows:
<?xml version="1.0"?>
<!-- events/MainEventApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*" >
    <mx:Script>
        <![CDATA[
            import myEvents.EnableChangeEvent;
            public function 
                enableChangedListener(eventObj:EnableChangeEvent):void {
                    // Handle event.
            }
        ]]>
    </mx:Script>
    <MyComp:MyButton enableChanged="myTA.text='got event';" />
    <mx:TextArea id="myTA" />
</mx:Application>
If you do not identify an event with the 
[Event]
 metadata tag, the compiler generates an 
error if you try to use the event name in MXML. The metadata for events is inherited from 
the superclass, however, so you do not need to tag events that are already defined with the 
[Event]
 metadata tag in the superclass.
Dispatching an event
You use the 
dispatchEvent()
 method to dispatch an event. The 
dispatchEvent()
 method 
has the following signature:
public dispatchEvent(event:Event):Boolean
This method requires an argument of type Event, which is the event object. The 
dispatchEvent()
 method initializes the 
target
 property of the event object with a reference 
to the component dispatching the event. 
You can create an event object and dispatch the event in a single statement, as the following 
example shows:
dispatchEvent(new Event("click")); 
You can also create an event object, initialize it, and then dispatch it, as the following example 
shows: 
var eventObj:EnableChangeEvent = new EnableChangeEvent("enableChange");