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

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

问题描述

我有一个JavaScript API,这应该是与GWT和Flex使用。使用FABridge它是很容易调用来自AS3 JavaScript方法,反之亦然。但是,当我尝试在我的JavaScript API注册回调到AS3方法,我会被卡住。下面是一个简短code样品:<​​/ P>

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 code。看来,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 + ");");
};

这一件作品很好地与IE浏览器,但没有其他的浏览器。比如在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!

有谁能告诉我,这是什么错误是关于(也许某种安全问题)?或者有没有人有一个更好的主意如何创建回调我可以通过JS调用AS3方法?

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错误你看到的我是pretty的肯定是安全错误(基于它来自哪里,在FF code)可能是$ P $从可以动态注射方法pventing您eval'ed没有JS跨preTER的方式获得。

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