Search code examples
gwtmozillajsni

GWT JSNI method call failing, but there are no errors


I'm trying to implement Mozilla's Persona in a GWT App. Here's part of the code from a dummy app I set up to test it:

public class OpenId implements EntryPoint {

private native void callWatch(String email)
/*-{
    $wnd.navigator.id.watch({
        loggedInUser: email,
        onlogin: function(assertion){
            $wnd.alert("Calling method");
            [email protected]::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.");
}
}

When I call the callWatch method, only the "Calling method" alert box shows up. Neither of the other ones are ever called. So for some reason the code appears to be stopping at the JSNI call right below the first alert. But there are no errors in Dev Mode.

I don't understand why the processLogin method doesn't get called.

I thought I followed Google's Documentation correctly.

I did try writing

[email protected]::processLogin(Ljava/lang/String;)(assertion);

as OpenID.@... and instance.@... due to this post.

I'm not sure what else to try.


Solution

  • The variable this points to the function that immediately surrounds it, which is in this case your onlogin JavaScript function. You need to use a temporary that variable (a typical JavaScript idiom, by the way)

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

    And then, ideally use $entry(...), so you will see error messages, if you have registered an UncaughtExceptionHandler.

    See also: https://stackoverflow.com/a/5235580/291741