User Guide

Adding custom properties and methods to a component 93
Defining properties and methods in ActionScript
With ActionScript, you define properties and methods by using the same syntax that you use
in an ActionScript class. For more information on using ActionScript to define properties and
methods, see Chapter 3, “Using ActionScript to Create Components,” on page 25.
When using ActionScript, you place a property or method definition within an
<mx:Script>
block. The
<mx:Script> tag must be an immediate child tag of the root tag of the MXML
file. A public variable declaration or a set function in an
<mx:Script> tag becomes a property
of the component. A public ActionScript function in an
<mx:Script> tag becomes a method
of the component.
In the following example, the component defines two data providers to populate the
ComboBox control, and a function to use as the event listener for the
creationComplete
event. This function sets the data provider of the ComboBox based on the value of the
shortNames property. By default, the shortNames property is set to true, to display two-
letter names:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/StateComboBoxPropAS.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="setNameLength();">
<mx:Script>
<![CDATA[
// Define public variables.
public var shortNames:Boolean = true;
// Define private variables.
private var stateArrayShort:Array = ["AK", "AL"];
private var stateArrayLong:Array = ["Arkansas", "Alaska"];
// Define listener method.
public function setNameLength():void {
if (shortNames) {
dataProvider=stateArrayShort; }
else {
dataProvider=stateArrayLong; }
}
]]>
</mx:Script>
</mx:ComboBox>