GWT JSNI - problem passing Strings

心已入冬 提交于 2019-11-28 10:22:53
Yusuf K.
private native void publish(EntryPoint p) /*-{
 $wnd.setText = function(from) {
  alert(from);
  p.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
 }
}-*/;

Could you give this code a try?

Chris Lercher

helloMethod is an instance method, and as such it requires the this reference to be set, when it is called. Your first example doesn't do this at call time. Your second example attempts to do this, but there's a little mistake, which one can make easily in JavaScript: The this reference in

$wnd.setText = function(from) {
  this.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};

points to the function itself. To avoid this, you'll have to do something like:

var that = this;
$wnd.setText = function(from) {
  that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from);
};

Or better:

var that = this;
$wnd.setText = $entry(function(from) {
  that.@com.example.my.Class::helloMethod(Ljava/lang/String;)(from)
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!