Search code examples
gwtjsni

GWT/JSNI this. is not replace with a reference to the instance


I have the following code in a GWT class:

private native JavaScriptObject getRequest() /*-{
    var request = ...;

    request.onOpen = function(response) {
[email protected]::onOpen(Lcom/mdarveau/fnp/client/Response;)(response);
    };

    return request;
}-*/;

void onOpen( Response response ) {
    Window.alert( "Got response " + response );
}

However, when the function associated with request.onOpen is called, the "this variable" is not replaced with a reference to the compiled GWT class. Using chrome javascript console and debugger I see that it looks like a reference to the request object.

Any idea why? I also have jquery loaded, could there be a conflict?


Solution

  • Got it: you need to get a reference to this in the native code and then use it in the function block:

    var theInstance = this;
    

    and then

    request.onOpen = function(response) {
        [email protected]::onOpen(Lcom/mdarveau/fnp/client/Res‌​ponse;)(response);
    };