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

查看:23
本文介绍了从 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);

第二个参数将始终为空.我所做的是通过 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 解释器进入的情况下进行评估的方法道路.

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天全站免登陆