0

Can anyone help with what is wrong in the code below(based on the answers to a similar question asked on SO):

    public String javaMethod(String input) {
        return "it works";
}

    public native void defineBridgeMethod() /*-{ 
        var that = this;
        $wnd.jsFunction= $entry(function(msg) {
                 [email protected]::javaMethod(Ljava/lang/String;)(msg)
            });
    }-*/;

The issue is that Javascript does not find jsFunction: alert(jsFunction) in Javascript code returns 'undefined'.

Thanks.

Edit: Huh, one hour later: figured out that I just needed to have that.@com... returned!

1
  • 1
    So your question is answered? If so, answer your own question please. Commented Mar 13, 2012 at 14:17

1 Answer 1

2

Huh, one hour later: figured out that I just needed to have that.@com... returned!

The bridge method should be:

public native void defineBridgeMethod() /*-{ 
    var that = this;
    $wnd.jsFunction= $entry(function(msg) {
             return [email protected]::javaMethod(Ljava/lang/String;)(msg)
        });
}-*/;
Sign up to request clarification or add additional context in comments.

2 Comments

I don't get what "var this = that" really does, but it works - while this.@... does not.
It has something to do with scoping. this within that function is another one than outside the function. You circumvent that by assigning the outer this to a variable which is also "visible" within the function statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.