GWT/JSNI这个.不替换为对实例的引用

Man*_*eau 2 gwt jsni

我在GWT类中有以下代码:

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

    request.onOpen = function(response) {
this.@com.mdarveau.fnp.client.Client::onOpen(Lcom/mdarveau/fnp/client/Response;)(response);
    };

    return request;
}-*/;

void onOpen( Response response ) {
    Window.alert( "Got response " + response );
}
Run Code Online (Sandbox Code Playgroud)

但是,当调用与之关联的函数时request.onOpen,"this变量"不会替换为对已编译的GWT类的引用.使用chrome javascript控制台和调试器我看到它看起来像是request对象的引用.

知道为什么吗?我也加载了jquery,可能会有冲突吗?

Man*_*eau 6

得到它:你需要this在本机代码中获得引用,然后在function块中使用它:

var theInstance = this;
Run Code Online (Sandbox Code Playgroud)

然后

request.onOpen = function(response) {
    theInstance.@com.mdarveau.fnp.client.Client::onOpen(Lcom/mdarveau/fnp/client/Res??ponse;)(response);
};
Run Code Online (Sandbox Code Playgroud)