SWFLoader can load a full application inside another. How do you get access its public properties and methods?
Here is an example for doing this…..
/** 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>
very interesting.
i’m adding in RSS Reader
Hi vinod,
A very good article i have come across.
Thanks Bruce……..
Thanks… music
Hey man, nice sample. It helped my a lot. Thanks for the post.
If you can write an e-mail in order to contact you to share codes and doubts I would be gratefull.
well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
This code was very helpful.
I am using SWFLoader to load a .swf from another source path, so when I use my .swf file name (Where you use SWFLoaded) I receive errors. If I eliminate strict type checking I can leave this off and it will compile and run fine. I would rather have strict type checking enabled any suggestions on how to work around this?