从Javascript调用Flex / AS3回调 [英] Calling a Flex/AS3 Callback from Javascript

查看:158
本文介绍了从Javascript调用Flex / AS3回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript API,应该可以使用GWT和Flex。使用FABridge,很容易从AS3调用Javascript方法,反之亦然。但是当我尝试注册一个回调到我的Javascript API中的AS3方法时,我被卡住了。这里是一个短代码示例:

I have a Javascript API, which should be usable with GWT and Flex. Using the FABridge it is really easy to call Javascript methods from AS3 and vice versa. But when I try to register a callback to an AS3 method in my Javascript API I get stuck. Here is a short code sample:

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);
}

简单的JS方法:

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

但是这个版本不工作,因为我总是在我的JS代码。看来FABridge正在削减其余的。
然后我尝试了一种不同的方法。我写了一个小的JS方法,它接受函数的名称,并从JS端创建回调。

But this version does not work, because I always receive an empty function in my JS code. It seems that the FABridge is cutting the rest. Then I tried a different approach. I wrote a little JS method, which takes the name of the function and creates the callback from the JS side.

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 + ");");
};

这个适用于Internet Explorer,但没有其他浏览器。例如在Firefox中,我得到以下错误消息:

This one works well with the Internet Explorer, but with no other browser. For example in Firefox I get the following error message:

NPMethod called on non-NPObject wrapped JSObject!

有人可以告诉我,这个错误是什么(可能是某种安全问题)?或者任何人都有一个更好的想法如何为我的AS3方法创建回调,可以由JS调用

Can somebody tell me, what this error is about (maybe some kind of security issue)? Or does anyone have a better idea how to create callbacks for my AS3 methods which can be called by JS?

推荐答案

功能不会在FABridge上串行化。

This is because functions don't serialize across the FABridge. Meaning in your

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

第二个参数将始终为null。我做的是通过eval在我的嵌入,因此添加回调的HTML页面上添加一个包装方法。所以你必须添加一个额外的,而烦人的步骤:

the second parameter will always be null. What I do is add a wrapper method on the HTML page via eval that points at my embed and therefore the added callback. So you have to add an extra, while annoying step:

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解释器阻碍的情况下进行eval方法的方法。

The NPObject error you're seeing I'm pretty sure is a security error ( based on where it comes from in the FF code ) probably preventing you from dynamically injecting methods that can be eval'ed without the JS interpreter getting in the way.

我甚至没有尝试编译上面所以,希望你能得到要点。

I haven't even tried to compile the above so, hopefully you get the gist.

这篇关于从Javascript调用Flex / AS3回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆