Flex / AS3 callback call from Javascript

I have a Javascript API that should be used with GWT and Flex. Using FABridge, it is very simple to call Javascript methods from AS3 and vice versa. But when I try to register the AS3 method callback in my Javascript API, I get stuck. Here is an example of a short code:

public function initApp():void {
    if (ExternalInterface.available) { 
        ExternalInterface.addCallback("foobar", foobar);
}
}

public function foobar():void {
    //the callback function
    Alert.show("Callback from API works!");
}

private function btnCallbackClicked():void {
    ExternalInterface.call("testAPICallbackFromJS", Application.application.foobar);
}

And a simple JS method:

function testAPICallbackFromGWT(callback){
  $clinit_26(); //added by the GWT compiler
  alert('callback to be launched 3 2 1');
  callback();
}

But this version does not work, because I always get an empty function in my JS code. FABridge seems to cut everything else. Then I tried a different approach. I wrote a little JS method that takes a function name and creates a callback from JS.

registerFlexCallback = function(registerMethod, callback, id) {
    /*
    workaround to create a callback for an AS method, which can be called by Javascript
        * registerMethod - Javascript method which shall be called for registration with the created callback as parameter
        * callback - AS method that shall be called by Javascript (available over the FABridge interface)
        * id - ID of the flash object (use Application.application.id in AS)
    */
    var swf = document.getElementById(id);
    eval(registerMethod + "(swf." + callback + ");");
};

This works well with Internet Explorer, but without another browser. For example, in Firefox, you receive the following error message:

NPMethod called on non-NPObject wrapped JSObject!

- , ( , - )? - , AS3-, JS?

+2
2

, FABridge.

ExternalInterface.call("testAPICallbackFromJS", Application.application.foobar);

. , , HTML eval, , , . , :

ExternalInterface.addCallback("foobar", foobar);

var callBack:String = "";
var functionName:String = UIDUtil.createUUID; 
callBack = "function " + functionName + "( ){ " + 
"document.getElementById('applicationName').foobar(arguments);"+
"}";
ExternalInterface.call("eval", callback);


ExternalInterface.call("testAPICallbackFromJS", functionName);

NPObject, , , ( , FF), , , JS .

, , , .

+2

.

-, , ExternalInterface , ExternalInterface .

public function initApp():void {
   if (ExternalInterface.available) { 
    ExternalInterface.addCallback("foobar", foobar);
}
}

timout, , , Externalinterface .

"foobar" javascript-. , , , "foobar", , .

function testAPICallbackFromGWT(callback){
  $clinit_26(); //added by the GWT compiler
  alert('callback to be launched 3 2 1');
  callback();
}

, .

// e.g. run just flash to javascript only
ExternalInterface.call("alert", "hello out there");

// establish the call from flash
ExternalInterface.addCallback("hello_out_there", foobar);

// and in javascript
alert(typeof('hello_out_there')); // will be 'function' if exists or undefined if ExternalInterface did not work

, .

, - javascript , . , , , , .

, JavaScript.

Firefox Internet Explorer, , IE - .

Firefox .

.

+1

Source: https://habr.com/ru/post/1737385/


All Articles