View comments | RSS feed

Using the Event metadata tag

You use the [Event] metadata tag to define events dispatched by a component so that the Flex compiler can recognize them as MXML tag attributes in an MXML file. You add the [Event] metadata tag in one of the following locations:

ActionScript components Above the class definition, but within the package definition, so that the events are bound to the class and not a particular member of the class.

MXML components In the <mx:Metadata> tag of an MXML file.

The Event metadata keyword has the following syntax:

[Event(name="eventName", type="package.eventType")]

The eventName argument specifies the name, including the package, of the event. The eventType argument specifies the class that defines the event.

The following example identifies the enableChanged event as an event that an ActionScript component can dispatch:

[Event(name="enableChanged", type="myEvents.EnableChangedEvent")]
public class MyComponent extends TextArea
{
    ...
}

The following example shows the [Event] metadata tag within the <mx:Metadata> tag of an MXML file:

<?xml version="1.0"?>
<!-- TextAreaEnabled.mxml -->
<mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml" >

    <mx:Metadata>
        [Event(name="enableChanged", type="myEvents.EnableChangedEvent")]
    </mx:Metadata>
    ....
</mx:TextArea>

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"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyComp="myControls.*" >
    <mx:Script>
        <![CDATA[
            import myEvents.EnableChangedEvent;

            public function
enableChangedListener(eventObj:EnableChangedEvent):void { ... } ]]> </mx:Script> <MyComp:TextAreaEnabled enableChanged="enableChangedListener(event)"/> </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.


Flex 2

Comments


lynxoid said on Jul 24, 2008 at 3:44 PM :
the line "The eventName argument specifies the name, including the package, of the event." should be changed to "The eventName argument specifies the name of the event." Part with "including the package" should go to the next sentence describing eventType.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/docs/00001645.html