我正在尝试在我的GWT项目中提供一些函数挂钩:
private TextBox hello = new TextBox();
private void helloMethod(String from) { hello.setText(from); }
private native void publish() /*-{
$wnd.setText = $entry(this.@com.example.my.Class::helloMethod(Ljava/lang/String;));
}-*/;
Run Code Online (Sandbox Code Playgroud)
publish()被召入onModuleLoad().但这不起作用,在开发控制台中没有提供反馈的原因.我也尝试过:
private native void publish() /*-{
$wnd.setText = function(from) {
alert(from);
this.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
}
}-*/;
Run Code Online (Sandbox Code Playgroud)
这将java.lang.ClassCastException在FireBug控制台中抛出一个,虽然alert火灾很好.建议?
helloMethod是一个实例方法,因此它需要在调用this时设置引用.您的第一个示例不会在通话时执行此操作.你的第二个例子尝试这样做,但是有一个小错误,可以在JavaScript中轻松搞定:引用this
$wnd.setText = function(from) {
this.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};
Run Code Online (Sandbox Code Playgroud)
指向函数本身.为避免这种情况,您必须执行以下操作:
var that = this;
$wnd.setText = function(from) {
that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};
Run Code Online (Sandbox Code Playgroud)
或更好:
var that = this;
$wnd.setText = $entry(function(from) {
that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from)
});
Run Code Online (Sandbox Code Playgroud)
private native void publish(EntryPoint p) /*-{
$wnd.setText = function(from) {
alert(from);
p.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
}
}-*/;
Run Code Online (Sandbox Code Playgroud)
你能尝试一下这段代码吗?
| 归档时间: |
|
| 查看次数: |
5596 次 |
| 最近记录: |