
64 Building a Flex Application
The previous example inserts the ActionScript code directly into the MXML code. Although
this technique works well for one or two lines of ActionScript code, for more complex logic
you typically define your ActionScript in an
<mx:Script> block, as the following example
shows:
<?xml version="1.0" encoding="utf-8"?>
<!-- ?xml tag must start in line 1 column 1 -->
<!-- MXML root element tag -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
// Define an ActionScript function.
private function duplicate():void {
myText.text=myInput.text;
}
]]>
</mx:Script>
<!-- Flex controls exist in a container. Define a Panel container. -->
<mx:Panel title="My Application">
<!-- TextInput control for user input. -->
<mx:TextInput id="myInput" width="150" text=""/>
<!-- Output TextArea control. -->
<mx:TextArea id="myText" text="" width="150"/>
<!-- Button control that triggers the copy. -->
<mx:Button id="myButton" label="Copy Text"
click="duplicate();"/>
</mx:Panel>
</mx:Application>
In this example, you implement the event listener as an ActionScript function. Flex calls this
function in response to the user selecting the Button control. This techniques lets you separate
your MXML code from your ActionScript code. You can also choose to divide your
application into multiple files to increase its modularity. For more information, see Chapter 7,
“Using ActionScript,” on page 91.
Kommentare zu diesen Handbüchern