Macromedia flex 2 Manual

Page of 254
106
Creating Advanced MXML Components
Passing a reference to the calling component
In 
, you passed a reference to a single 
component to the custom MXML component. This allowed the MXML component to access 
only a single component in the main application.
One type of reference that you can pass to your component is a reference to the calling 
component. With a reference to the calling component, your custom MXML file can access 
any properties or object in the calling component. 
To pass a reference to the calling component to a custom MXML component, you create a 
property in the custom MXML component to represent the calling component. Then, from 
the calling component, you pass a reference to the calling component to the custom MXML 
component, as the following example shows:
<?xml version="1.0"?>
<!-- mxmlAdvanced/CallingComponent.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    horizontalAlign="left" 
    xmlns:MyComp="myComponents.*">
    <!-- Use the caller property to pass a reference to the 
        calling component to DestinationComp. -->
    <mx:Label text="Enter text"/>        
    <mx:TextInput id="text1" text="Hello"/>
    <mx:Label text="Input text automatically copied to MXML component."/>        
    <MyComp:DestinationComp caller="{this}"/>
</mx:Application>
In the definition of DestinationComp.mxml, you define the 
caller
 property, and specify as 
its data type the name of the file of the calling MXML file, as the following example shows: 
<?xml version="1.0"?>
<!-- mxmlAdvanced/myComponents/DestinationComp.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            // Define variable to reference calling file. 
            [Bindable]
            public var caller:CallingComponent;
        ]]>
    </mx:Script>
    
    <mx:TextInput id="mytext" text="{caller.text1.text}"/>
</mx:VBox>