【问题标题】:GWT JSNI method call failing, but there are no errorsGWT JSNI 方法调用失败,但没有错误
【发布时间】:2012-11-09 23:57:22
【问题描述】:

我正在尝试在 GWT 应用程序中实现 Mozilla 的 Persona。这是我为测试它而设置的虚拟应用程序的部分代码:

public class OpenId implements EntryPoint {

private native void callWatch(String email)
/*-{
    $wnd.navigator.id.watch({
        loggedInUser: email,
        onlogin: function(assertion){
            $wnd.alert("Calling method");
            this.@com.gallup.openid.client.OpenId::processLogin(Ljava/lang/String;)(assertion);
            $wnd.alert("Called Java Method");
        },
        onlogout: function(){alert("Logged Out!");}
    });
}-*/;

private void processLogin(String assertion){
    Window.alert("Logged in!");
    personaStatus.setText("Log In Complete.");
}
}

当我调用callWatch 方法时,只显示“调用方法”警告框。其他任何一个都没有被调用过。因此,由于某种原因,代码似乎在第一个警报下方的 JSNI 调用处停止。但是开发模式没有错误。

我不明白为什么 processLogin 方法没有被调用。

我以为我正确地关注了Google's Documentation

我确实尝试过写作

this.@com.gallup.openid.client.OpenId::processLogin(Ljava/lang/String;)(assertion);

由于this postOpenID.@...instance.@...

我不确定还能尝试什么。

【问题讨论】:

  • 看起来断言变量可能存在问题... '$wnd.alert(assertion);'显示一个字符串。如果是这样,您是否尝试过将静态字符串传递给您的 processLogin 函数。

标签: gwt mozilla jsni


【解决方案1】:

变量this 指向直接包围它的函数,在本例中是您的onlogin JavaScript 函数。您需要使用一个临时的 that 变量(顺便说一下,这是一个典型的 JavaScript 习惯用法)

private native void callWatch(String email)
/*-{
  var that = this;
   ...
    onlogin: function(assertion){
      that.@com...

然后,最好使用$entry(...),这样如果您注册了 UncaughtExceptionHandler,您将看到错误消息。

另请参阅:https://stackoverflow.com/a/5235580/291741

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多