16
Creating Advanced Components
For example, you define a component called ModalText as the following example shows:
[Event("myEvent")]
class ModalText extends UIComponent {
...
}
Within the body of your class definition, you then use the
dispatchEvent()
method to dispatch
myEvent
. You can then handle the event in MXML as the following example shows:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*" >
<mx:Script>
<![CDATA[
function handleText(event)
{
...
}
]]>
</mx:Script>
<ModalText ...
textChanged="handleText(event);"
/>
</mx:Application>
For more information on events, see
Developing Flex Applications
.
Defining event handlers for child components
Custom components implement the
createChildren()
method to create subobjects (such as
other components) in the custom component. To create a subobject, you use the
createClassObject()
method, as the following example shows:
function createChildren():Void {
createClassObject(TextInput, "text_mc", 0, {preferredWidth: 80,
editable:false});
text_mc.addEventListener("change", this);
}
This example creates a TextInput control as a child of your custom component. Note that the
createChildren()
method also contains a call to the
addEventListener()
method to register
an event handler for the
change
event generated by the TextInput control.
You can define the event handler function that listens for your events in your component’s
ActionScript definition. The following example handles the
change
,
focusOut
, and
click
events
of the children of the component:
function handleEvent(evt:Object):Void {
if (evt.type == "change") {
dispatchEvent({ type: "change" });
} else if (evt.type == "focusOut") {
text_mc.editable = false;
} else if (evt.type = "click") {
text_mc.editable = !text_mc.editable;
}
}
By default, Flex calls the
handleEvent()
method of a component whenever it generates an event.
Содержание FLEX-CREATING ADVANCED COMPONENTS
Страница 1: ...Creating Advanced Components...
Страница 4: ...4 Contents...
Страница 24: ...24 Creating Advanced Components...