我在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,可能会发生冲突吗?
答案 0 :(得分:6)
知道了:你需要在本机代码中获得对this
的引用,然后在function
块中使用它:
var theInstance = this;
然后
request.onOpen = function(response) {
theInstance.@com.mdarveau.fnp.client.Client::onOpen(Lcom/mdarveau/fnp/client/Response;)(response);
};