GWT JSNI - problem passing Strings

前端 未结 2 363
清歌不尽
清歌不尽 2020-12-10 07:54

I\'m trying to provide some function hooks in my GWT project:

private TextBox hello = new TextBox();
private void helloMethod(String from) { hello.setText(fr         


        
相关标签:
2条回答
  • 2020-12-10 08:28

    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)
    });
    
    0 讨论(0)
  • 2020-12-10 08:42
    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?

    0 讨论(0)
提交回复
热议问题