Lee Loftiss
Lee Loftiss

Reputation: 3195

Accessing a class from an externally loaded Flex SWF

Using Flex 4, I have created an SWF that has the following code:

<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;
        import com.Index ;

        protected function start(event:FlexEvent):void
        {
            this.addElement (new Index());
        }
    ]]>

What I am trying to do is load the SWF into another SWF and access this class. The problem is the Flex class Main is what is recognized by the loading SWF. I have tried accessing its children(elements), but the added item is not there.

My ultimate goal is to load an SWF that has an object that can interact with the loading SWF.

lee

Addition:

Thanks for your addition. The code is working now, but it only access the Flex class created by the MXML. Here is the complete MXML:

<?xml version="1.0" encoding="utf-8"?>

           width="1000"
           height="700"
           creationComplete="start(event)"
           >

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;
        import Test;

        protected function start(event:FlexEvent):void
        {
            this.addElement(new Test ());
        }
    ]]>

To try and extract the class Test, I did the following:

var MainC:Class = L.contentLoaderInfo .applicationDomain .getDefinition ("Main.Test") as Class;
        var O:Object = new MainC();

This returns 'Error #1065: Variable Test is not defined.'

I get the same result if I use ...getDefinition("Test").

If I use ....getDefinition("Main"), it works fine. But I cannot seem to reach into the class it creates to extract anything.

lee

Upvotes: 1

Views: 1376

Answers (1)

rekaszeru
rekaszeru

Reputation: 19220

The best way to go is to have that swf as a library (swc) and link it to your project. That way you could achieve strong typing, and access the underlying classes.

If you don't have access to neither the library release of the swf object, nor it's source to build the swc library yourserlf, you can still use the external classes, but not with strong typing:

var ExtIndex : Class = loader.contentLoaderInfo.applicationDomain.getDefintiion("Index");
var instance : Object = new ExtIndex();
this.addElement(instance);

so you must define your instance as Object.

Although, in case the Index class in your swf implements an interface that you know of (you have it in your project), like I_Index, you can use it instead of Object:

var instance : I_Index = new ExtIndex();
this.addElement(instance);

You might also find this article useful in deciding which way to go.

Update 1

Declaring the loader in Flex:

var loader: flash.display.Loader;
[...]
var ExtIndex: Class = loader.contentLoaderInfo.applicationDomain.
    getDefinition("your.package.Index") as Class;

In case you have your Index class in a package (other than the default), you should specify it in its name. The code above searches for the Index class inside the your/package package.

API Docs for getDefinition:

Gets a public definition from the specified application domain. The definition can be that of a class, a namespace, or a function.

Parameters:
name The name of the definition.
Returns: The object associated with the definition.
Throws: ReferenceError - No public definition exists with the specified name.

Language Version: 3.0
Player Version: Flash 9, AIR 1.0

Update 2
To make the Test class defined in your external application accessible from other flex applications by loading the swf that contains it, you must have a variable declared with that type inside your swf's main application. Otherwise that class won't be compiled into the swf (to save the size of it).
Flex only compiles classes actually in use into the swf, so it's not enough to just import a class.
In the main application of your swf you should declare:

var _tmp:Index;

This makes sure that the Index class will be compiled into your swf, and you can access it from an other application which loads that swf.

Update 3
You can check out how this works at this link. The source code is included too.

Since there are two projects, in the [src] folder you can see the test's source, and in the [source path src] folder the source of the externally loaded swf file.
If you download the whole project, make sure that you separate the two projects.

Upvotes: 3

Related Questions