Macromedia flex 2 Manual
Adding custom properties and methods to a component
105
Passing a reference to the component
A loosely coupled component is a highly reusable component that you can easily use in
different places in one application, or in different applications. To make the component from
different places in one application, or in different applications. To make the component from
reusable, you can pass a reference
to the
TextArea
control to the custom component, as the following example shows:
<?xml version="1.0"?>
<!-- mxmlAdvanced/MainPassRefToTA.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<mx:TextArea id="myTAMain" />
<MyComp:StateComboBoxPassRefToTA outputTA="{myTAMain}" />
</mx:Application>
<!-- mxmlAdvanced/MainPassRefToTA.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*">
<mx:TextArea id="myTAMain" />
<MyComp:StateComboBoxPassRefToTA outputTA="{myTAMain}" />
</mx:Application>
The custom component does not have to know anything about the main application, other
than that it writes its results back to a
than that it writes its results back to a
TextArea
control, as the following example shows:
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/StateComboBoxPassRefToTA.mxml -->
<!-- mxmlAdvanced/myComponents/StateComboBoxPassRefToTA.mxml -->
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"
close="handleCloseEvent(event);">
close="handleCloseEvent(event);">
<mx:Script>
<![CDATA[
import flash.events.Event;
import mx.controls.TextArea;
// Define a variable of type mx.controls.TextArea.
public var outputTA:TextArea;
public function handleCloseEvent(eventObj:Event):void {
outputTA.text=String(this.selectedIndex);
}
]]>
</mx:Script>
<mx:dataProvider>
<mx:String>AK</mx:String>
<mx:String>AL</mx:String>
</mx:dataProvider>
</mx:ComboBox>
<![CDATA[
import flash.events.Event;
import mx.controls.TextArea;
// Define a variable of type mx.controls.TextArea.
public var outputTA:TextArea;
public function handleCloseEvent(eventObj:Event):void {
outputTA.text=String(this.selectedIndex);
}
]]>
</mx:Script>
<mx:dataProvider>
<mx:String>AK</mx:String>
<mx:String>AL</mx:String>
</mx:dataProvider>
</mx:ComboBox>
In this example, you use the Flex data binding syntax to pass the reference to the TextArea
control to your custom component. Now, you can use StateComboBoxPassRefToTA.mxml
anywhere in an application. The only requirement is that the calling component must pass a
reference to a TextArea control to the component.
control to your custom component. Now, you can use StateComboBoxPassRefToTA.mxml
anywhere in an application. The only requirement is that the calling component must pass a
reference to a TextArea control to the component.