2

我在 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 );
}

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

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

4

1 回答 1

6

明白了:您需要this在本机代码中获取对的引用,然后在function块中使用它:

var theInstance = this;

进而

request.onOpen = function(response) {
    theInstance.@com.mdarveau.fnp.client.Client::onOpen(Lcom/mdarveau/fnp/client/Res‌​ponse;)(response);
};
于 2012-05-05T03:08:08.853 回答