As a Flex developer, you will find that ActionScript’s primary use will be working with visual controls and containers in your application. In this section, you learn how to reference a Flex control and manipulate the properties in ActionScript.
Referring to a Control in ActionScript
Before you write any ActionScript, you must define a control in MXML and assign an id property to it. This allows you to reference the Flex control in ActionScript.
<mx:TextInput id="myTextInput"
text="Refer to me via the id property" />
The id property is optional and is only required when referencing the component. The MXML compiler creates a variable called myTextInput that contains a reference to the TextInput object. You can access all the public methods and properties using the dot notation in ActionScript.
To access the control in ActionScript, you must reference the id property. The following example shows you how this is done:
<?xml version="1.0"
encoding="utf-8"?> <mx:Application xmlns:
mx="http://www.adobe.com/2006/mxml" verticalAlign="middle"
horizontalAlign="center"
xmlns="*">
<mx:Script> <![CDATA[
public function getText():String
{
return myTextInput.text; } ]]>
</mx:Script> <mx:TextInput id="myTextInput"
text="Refer to me via the id property" />
</mx:Application>
The getText() method returns a string, refers to the TextInput control’s id property (myTextInput) and accesses the text property of the object.
One thing to note is that the ids for all tags in an MXML component, even deeply nested, generate public variables in the component being defined. In other words, all id properties must be unique within the document.
If the id property of a Flex component is not defined, you can use the methods of the component’s container, namely, getChildAt() and getChildByName():
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http:
//www.adobe.com/2006/mxml"
verticalAlign="middle"
horizontalAlign="center">
<mx:Script>
<![CDATA[ import mx.controls.Alert;
import mx.controls.TextInput;
public function getText():String
{ var name:String = TextInput(getChildAt(0)).name;
var text:String = TextInput(getChildByName(name)).text;
return text; } ]]>
</mx:Script> <mx:TextInput text="Hello World!" />
<mx:Button label="Get Text" click="Alert.show(getText())" />
</mx:Application>
This example casts the getChildAt(0) to type TextInput to access the name property and assigns the value to the variable name. The variable name is passed into the childChildByName(name) method and cast to type TextInput to allow access to the text property, which is assigned to variable text.
The this keyword is another way to reference components in a document using ActionScript. To access the object, you use the keyword, followed by the square brackets, passing in a string between them.
The following code example has the same functionality as the previous one, except it uses the this keyword to reference the TextInput control:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml
verticalAlign="middle"
horizontalAlign="center"> <mx:Script>
<![CDATA[ import mx.controls.Alert;
import mx.controls.TextInput;
public function getText():String {
return this["myTextInput"].text; }
]]> </mx:Script>
<mx:TextInput id="myTextInput" text="Hello World!" />
<mx:Button label="Get Text"
click="Alert.show(getText())" /> </mx:Application>