Macromedia Flex Macromedia Flex
Communicate with App loaded by SWFLoader
  Home

Jul 06, 2007 - Communicate with App loaded by SWFLoader
Shows event handlers necessary to know when loaded app can be accessed.

SWFLoader can load a full application inside another.  On issue with an app so loaded is how do you communicate with it.  How do you get access its public properties and methods?  And when is that app ready to be accessed?  It is a bit more complicated than you might think, but here is a working example.

Tracy

/** SWFLoaded.mxml */
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="yellow">
<mx:Script><![CDATA[
  [Bindable]public var varOne:String = "This is a public variable";

  public function setVarOne(newText:String):void {
      varOne=newText;
  }//setVarOne
]]></mx:Script>
  <mx:Label id="lblOne" text="I am here"/>
  <mx:Label text="{varOne}"/>
  <mx:Button label="Nested Button"
      click="setVarOne('Nested button pressed.');"/>
</mx:Application>

/** SWFLoaderTest.mxml */
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script><![CDATA[
  import mx.events.FlexEvent;
  import mx.controls.Alert;
  import mx.managers.SystemManager;
  import mx.controls.Label;
  import mx.core.Application;
 
  [Bindable]public var _appLoaded:Application;
 
  /** run by SWFLoader.complete event */
  private function onCompleteAppLoader(oEvent:Event):void
  {
    var smAppLoaded:SystemManager = SystemManager(oEvent.target.content);  //get a ref to the loaded app
    //listen for the application.complete event
    smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onCurrentApplicationComplete);
  }//onCompleteAppLoader
 
  /** run by application.complete event */
  private function onCurrentApplicationComplete(oEvent:Event):void
  {
    _appLoaded = Application(oEvent.target.application);
    var sUrl:String = _appLoaded.url;
    SWFLoaded(_appLoaded).setVarOne("This value set from main app!"); //without cast, strict check compiler complains
    lbl.text = "This value from loaded app:" + _appLoaded.url;
  }//onCurrentApplicationComplete 
 
         
  /** Update the Label control in the outer application
   * from the Label control in the loaded application. */
  public function updateLabel():void {
      lbl.text=_appLoaded["lblOne"].text;
  }//updateLabel
 
  /** Write to the Label control in the loaded application.*/
  public function updateNestedLabels():void {
      _appLoaded["lblOne"].text = "I was just updated";
      _appLoaded["varOne"] = "I was just updated";
  }//updateNestedLabels
 
  /** Write to the varOne variable in the loaded application
  * using the setVarOne() method of the loaded application.*/
  public function updateNestedVarOne():void {
      SWFLoaded(_appLoaded).setVarOne("Updated varOne!");
  }//updateNestedVarOne
]]></mx:Script>

  <mx:Label id="lbl"/>
  <mx:SWFLoader id="myLoader" width="300"
      complete="onCompleteAppLoader(event);"/>
 
  <mx:Button label="Load SWF"
      click="myLoader.source='SWFLoaded.swf';"/>   
  <mx:Button label="Update Label Control in Outer Application"
      click="updateLabel();"/>
 
  <mx:Button label="Update Nested Controls" click="updateNestedLabels();"/>
 
  <mx:Button label="Update Nested varOne" click="updateNestedVarOne();"/>
</mx:Application>

 

File Details
Created On Jul, 06, 2007 by Tracy Spratt
Last Modified On Jul, 06, 2007 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: General
Type: Complete Lesson
Difficulty: Intermediate
Keywords: