Flex Calling Methods of a Loaded Flex Movie

May 08, 2009

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>

9 comments

  1. try declaring loaderManager as an object and then try this:

    var loaderManager:Object = loader.content as Object;
    loaderManager.sayHello('todd');

    Comment by todd sharp – May 08, 2009
  2. I think u should change to Event.COMPLETE to Event.INIT
    swfLoader.addEventListener( Event.INIT, swfInit );

    Comment by Armen – May 08, 2009
  3. Hi Todd, Thanks for the input!
    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
  4. Hi Armen, thanks for the comment. I pretty new to Flex so all feedback is really useful :)

    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
  5. First u should add init method to swf loader.
    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
  6. Thanks for the clarification Armen - that all makes sense :)

    Comment by John Whish – May 12, 2009
  7. could you resolved the problem??? I'm in the same situation

    Comment by Figux – May 13, 2009
  8. Hi Figux, the code in my post works fine. In my production code I abstract out the code which checks and gets the function from the loaded swf into another class so that I can just do something like this:

    loadedSwf = new LoadedApplication( loadedTemplateManager.application );
    if ( loadedSwf.hasMethod( "sayHello" ) )
    {
    loadedSwf.execute( "sayHello", arguments );
    }

    Comment by John Whish – May 14, 2009
  9. Thanks for your answer.

    Comment by Figux – May 14, 2009

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.