Tor Rent
Tor Rent

Reputation: 1

How to autofill flex input by using JavaScript without flashvars?

I have flex application (swf file). Does anyone one know how to autofill flex textInput from JavaScript without using flashVars? It must work in FireFox and IE.

Upvotes: 0

Views: 299

Answers (1)

Maxim Kachurovskiy
Maxim Kachurovskiy

Reputation: 3022

You can write a wrapper.swf and load main.swf into it.

MyApp.mxml - Flex project application file

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:HGroup id="hGroup" width="100%" height="100%" 
        horizontalAlign="center" verticalAlign="middle">
        <s:TextInput id="textInput" text=""/>
    </s:HGroup>
</s:Application>

Wrapper.as - ActionScript project application file

package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import mx.core.mx_internal;

use namespace mx_internal;

public class Wrapper extends Sprite
{

    private var loader:Loader;

    private var application:Object;

    public function Wrapper()
    {
        loader = new Loader();
        addChild(loader);
        loader.contentLoaderInfo.addEventListener(Event.INIT, loader_initHandler);
        loader.load(new URLRequest("MyApp.swf"), 
            new LoaderContext(false, ApplicationDomain.currentDomain));
    }

    private function loader_initHandler(event:Event):void
    {
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    private function enterFrameHandler(event:Event):void
    {
        try
        {
            application = ApplicationDomain.currentDomain.getDefinition(
                "mx.core.FlexGlobals").topLevelApplication;
        }
        catch (error:*)
        {
            return; // app is not ready yet
        }
        if (!application)
            return;

        removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        if (application.initialized)
            injectText();
        else
            application.addEventListener("creationComplete", application_creationCompleteHandler);
    }

    private function injectText():void
    {
        var textInput:Object = application.getElementAt(0).getElementAt(0);
        if (textInput)
            textInput.text = "Custom value";
    }

    private function application_creationCompleteHandler(event:Event):void
    {
        injectText();
    }

}
}

Result

Finally, you can add any logic in wrapper including ExternalInterface.addCallback.

Upvotes: 1

Related Questions