GWT - 从外部JavaScript调用实例方法 [英] GWT - Calling instance method from external javascript

查看:104
本文介绍了GWT - 从外部JavaScript调用实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在GWT中使用$ entry方法来允许外部javascript执行java方法。
您可以在他们的文档中看到解释 https ://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI?hl = fr#calling



然而,这里的例子只有用静态方法。我试图为它写一个非静态方法,当我尝试调用它时,我得到一个异常:

  java.lang.ClassCastException:无法将com.google.gwt.core.client.JavaScriptObject $转换为mypackage.MyModule 



下面是我的代码:

  public native void setRefreshModuleCallback()/ *  -  {
$ wnd.refreshModule = $ entry(function(){
this。@ mypackage.MyModule :: refreshModuleJava();
alert('test');
});
} - * /;

public void refreshModuleJava(){
logger.log(Level.WARNING,REFRESH);
}

我发现非常有趣的是,alert被调用,我看到结果浏览器,但之前的调用不会执行。



你知道是否真的有可能做这种事吗?

解决方案

$ entry 不是关于调用java ,而是关于确保一些事情顺利进行在GWT中:异常被路由到 GWT.UncaughtExceptionHandler ,并通过 Scheduler#scheduleEntry Scheduler#scheduleFinally 被正确调用。 您的问题是 this 。当函数被调用时,这个不是你的 MyModule 类(它很可能是 $ wnd object)。这就是为什么你链接的问题使用 var that = this 。这是关于范围



您还需要实际调用方法,不仅仅引用它:在JSNI中,第一对parens用于形式参数(以消除重载),并且您需要另一对传递实际参数:。@ mypackage.MyModule :: refreshModuleJava()()


There is this $entry method that we can use in GWT to allow external javascript to execute java methods. You can see the explanations in their documentation https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI?hl=fr#calling

However, the example there is only with static methods. I'm trying to write it for a non-static method and when I try to call it, I get an exception :

java.lang.ClassCastException: Cannot cast com.google.gwt.core.client.JavaScriptObject$ to mypackage.MyModule

Here is my code :

public native void setRefreshModuleCallback() /*-{
    $wnd.refreshModule = $entry(function() {
        this.@mypackage.MyModule::refreshModuleJava();
        alert('test');
    });
}-*/;

public void refreshModuleJava() {
    logger.log(Level.WARNING, "REFRESH");
}

What I find very funny is that alert is called, I see the result in the browser, but the call just before is not performed.

Do you know if it's actually possible to do such thing ?

解决方案

$entry is not about calling java, it's about ensuring a few things go well in GWT: exceptions are routed to the GWT.UncaughtExceptionHandler, and commands scheduled via Scheduler#scheduleEntry and Scheduler#scheduleFinally are correctly called.

Your problem is the this. When the function is called, this is not your MyModule class (it's most probably the $wnd object). This is why the question you linked to uses var that = this. It's about scoping.

You also need to actually call the method, not only reference it: in JSNI, the first pair of parens are for the formal parameters (to disambiguate overloads), and you need another pair passing the actual arguments: that.@mypackage.MyModule::refreshModuleJava()().

这篇关于GWT - 从外部JavaScript调用实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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