Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you dynamically call a function in Actionscript 3 from Javascript at runtime without using eval()?

I'm trying to build an API in JS that will perform some operations and then execute the callback that's registered in AS when it's done. Because it's an API, I am just providing a JS method signature for another developer to call in Flash. Thus, the callback name that's registered in the AS part of the code should be a parameter that's passed in to the JS API in order for JS to communicate back to Flash.

For example:

[AS3 code]

ExternalInterface.addCallback("flashCallbackName", processRequest);
ExternalInterface.call("namespace.jsFnToCall", flashCallbackName);

function processRequest(data:String):void
{
    //do stuff
}

[JS code]

var namespace =
{
    jsFnToCall: function(callback)
    {
        //Do stuff in this function and then fire the callback when done.

        //getFlashMovie is just a util function that grabs the
        //Flash element via the DOM; assume "flash_id"'s a global var
        //Below does not work...it's what I'd be ideally be doing some how.
        getFlashMovie(flash_id).callback(data);
    }
};

Because the definition of the function is in AS, I can't use the window[function name] approach. The only way I can think of is to build the callback in a string and then use the eval() to execute it.

Suggestions? T.I.A.

like image 947
D C Avatar asked Apr 09 '26 05:04

D C


1 Answers

Well, I can think of one thing I would try, and one thing that would work.

What I would try first.

getFlashMovie(flash_id)['callback'](data);

What would work: Have the callback always be the same, say callback. The first parameter to the callback could be used to determine what actual function to call in flash. For example:

function callback($fn:String, $data:*) {

    // either
    this[$fn]($data);
    // or
    switch ($fn) {
        case "callback1":
            DoSomeCallback($data);
            break;
}
like image 169
sberry Avatar answered Apr 11 '26 18:04

sberry