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
");
Содержание FLEX 2 - CREATING AND EXTENDING COMPONENTS
Страница 1: ...Creating and Extending Flex 2 Components Adobe Flex 2...
Страница 6: ...6 Contents...
Страница 10: ...10 About Flex Documentation...
Страница 12: ......
Страница 24: ...24 Creating Flex Components...
Страница 74: ...74 Compiling Components...
Страница 76: ......
Страница 118: ...118 Creating Advanced MXML Components...
Страница 120: ......
Страница 182: ...182 Creating Advanced Visual Components in ActionScript...
Страница 194: ...194 Creating Custom Style Properties...
Страница 204: ...204 Creating Template Components...
Страница 206: ......
Страница 216: ...216 Creating Custom Formatters...
Страница 254: ...254 Index...