Accessing sub-applications from the main application

Although you can access the sub-application from the main application, you cannot directly reference methods and properties. This is because the members of the sub-application, unless it is embedded at compile-time, are not known by the compiler when the main application compiles.

You can, however, get a reference to the sub-application’s SystemManager. You can then treat members of the sub-application as dynamic properties and methods of the sub-application’s object.

The following example loads a remote sub-application into the SWFLoader. It sets the value of the trustContent property to true so that the sub-application is loaded into the same security domain as the main application. It casts the content property of the SWFLoader to a SystemManager so that the application’s members can be accessed dynamically. It then calls a method and accesses a property of the sub-application.

<?xml version="1.0" encoding="utf-8"?>
<!-- apploading/MainAppUsingSubAppMembers.mxml -->
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:layout> 
        <s:VerticalLayout/> 
    </s:layout>

     <fx:Script>
          <![CDATA[
               import mx.managers.SystemManager;

               private function getValueFromSubApp():void { 
                    /* Cast the SWFLoader's content as a SystemManager 
                      to access the sub-application. */
                    var subApp:SubApp2 = 
                         (contentLoader.content as SystemManager).application as SubApp2;

                    /* Call a method and access a property of the 
                      sub-application. */
                    label1.text = subApp.doSomething() + subApp.answer;
               }
          ]]>
     </fx:Script>
     
     <mx:SWFLoader id="contentLoader" visible="false" 
          height="0" width="0" 
          trustContent="true" 
          source="http://yourdomain.com/SubApp2.swf"/>   

     <mx:Panel id="myPanel2" 
          paddingLeft="10" paddingBottom="10" 
          paddingTop="10" paddingRight="10">
          <s:Label id="label1"/>
          <s:Button id="b2" label="Call SubApp2" click="getValueFromSubApp()"/>
     </mx:Panel>
     
</s:Application>
The following example shows the sub-application that is loaded by the main application in the previous example. It defines a public property, answer, as well as a public method, doSomething(), that returns a String.
<?xml version="1.0" encoding="utf-8"?>
<!-- apploading/SubApp2.mxml -->
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
    
    <fx:Script>
          <![CDATA[
               // Define a public property.
               public var answer:int = 42;
          
               // Define a public method that returns a String.
               public function doSomething():String {
                    return "The answer is ";
               }
          ]]>
     </fx:Script>
</s:Application>
For more information about accessing sub-applications from the main application, see SWFLoader control.

If the sub-application is loaded remotely, you can access its members only if it is in the same security domain as the main application, or if you and the sub-application call the Security.allowDomain() method. In this case, you must call the Security.allowDomain() method from the main application on the sub-application’s domain, and the sub-application must call this method on the main application’s domain. You must also call the allowDomain() method early in the sub-application’s lifecycle. For example, call it in a preinitialize event handler.