Flex Calling Methods of a Loaded Flex Movie
I had trouble coming up with the post title for this one! I'm currently building a Flex application that dynamically loads in another Flex application and calls it's methods. When I started building it that seemed like it should be a pretty straight forward thing to do (and maybe there is a better way!).
Here is sample source code for the Flex application that loads in the 2nd Flex application.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="220" height="120"
applicationComplete="init();">
<mx:Script>
<![CDATA[
import mx.managers.SystemManager;
import mx.controls.Alert;
import mx.events.FlexEvent;
/*
--------------------------------------------------------------------------------
Class Properties
--------------------------------------------------------------------------------
*/
/* Public Properties
-------------------------------------------------------------------------------- */
public var loaderManager:SystemManager;
/*
--------------------------------------------------------------------------------
Pseudo Constructor
--------------------------------------------------------------------------------
*/
private function init():void
{
// load the swf into the SWFLoader
swfLoader.load( "NestedApplication.swf" );
// listen for when the swf has loaded
swfLoader.addEventListener( Event.COMPLETE, swfLoaded );
}
/*
--------------------------------------------------------------------------------
Private Methods
--------------------------------------------------------------------------------
*/
private function swfLoaded( e:Event ):void
{
loaderManager = swfLoader.content as SystemManager;
// must wait for the swf application to finish loading
// otherwise the application is null
loaderManager.addEventListener( FlexEvent.APPLICATION_COMPLETE, swfReady );
}
private function swfReady( e:FlexEvent ):void
{
// get a refence to the loaded flex application
var loadedApp:Application = Application( loaderManager.application ) ;
var sayHello:Function;
// check for the method in loaded swf
if ( loadedApp.hasOwnProperty( "sayHello" ) )
{
// create function
sayHello = loadedApp[ "sayHello" ] as Function;
sayHello( "aliaspooryorik" );
}
else
{
Alert.show("no such method");
}
}
]]>
</mx:Script>
<mx:SWFLoader id="swfLoader" x="10" y="10" width="200" height="100"/>
</mx:Application>
This is the loaded Flex application with the public method I want to call.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="200" height="100" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
public function sayHello( whom:String ):void
{
myTextBox.text = "Hello " + whom + "!";
}
]]>
</mx:Script>
<mx:Text id="myTextBox"
text="Anyone there?"
x="10" y="37" width="180" height="25" fontSize="16"/>
</mx:Application>
- Posted in:
- Flex


var loaderManager:Object = loader.content as Object;
loaderManager.sayHello('todd');
Comment by todd sharp – May 08, 2009
swfLoader.addEventListener( Event.INIT, swfInit );
Comment by Armen – May 08, 2009
I tried changing the swfReady method to this:
private function swfReady( e:FlexEvent ):void
{
var app:Object = swfLoader.content as Object;
app.sayHello('todd');
}
but I get a runtime error of "...Property sayHello not found..."
Shame as the code is much nicer :) Am I missing something?
Comment by John Whish – May 08, 2009
I'm not sure if using Event.INIT instead Event.COMPLETE will make any difference in this example as really I'm waiting for FlexEvent.APPLICATION_COMPLETE. Please correct me if I'm wrong!
For anyone reading that's interested I checked the differences and they are:
complete - Dispatched when data has loaded successfully.
init - Dispatched when the properties and methods of a loaded SWF file are accessible (ex. when only first frame was loaded).
Comment by John Whish – May 08, 2009
Inside init method u can add application complete event to the content of loader which is ur loaded appl. after complete u can call public methods of loaded appl.
loader.content points to root of loaded appl, which is Systemmanager. SystemManager.application points to the application of loaded swf which contains ur public method.
The most important thing here is to understand that SystemManager.application will be null when its just loaded.
that's why u have to wait for init event, after wait for APPLICATION_COMPLETE event, and after call public methods.
Hope that will help.
Comment by Armen – May 09, 2009
Comment by John Whish – May 12, 2009
Comment by Figux – May 13, 2009
loadedSwf = new LoadedApplication( loadedTemplateManager.application );
if ( loadedSwf.hasMethod( "sayHello" ) )
{
loadedSwf.execute( "sayHello", arguments );
}
Comment by John Whish – May 14, 2009
Comment by Figux – May 14, 2009
this sounds like it might solve a problem for me, but when I tried it I got a null pointer on this line in the swfLoaded method:
loaderManager.addEventListener( FlexEvent.APPLICATION_COMPLETE, swfReady );
When I debugged, it seems that swfLoader.content has a reference but loaderManager is still null after the assignment.
Has anyone else had this problem? Or have any thought on why it is happening for me?
Regards,
Ben.
Comment by Ben – March 17, 2010
Comment by puneet – January 17, 2012