Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT- How to call an INSTANCE Java Method from Handwritten JavaScript?

Tags:

gwt

I need to call an instance Java method from handwritten Javascript. In the GWT docs it is explained how to do this with static methods and classes and it works fine:

http://code.google.com/p/google-web-toolkit-doc-1-6/wiki/DevGuideJavaFromJavaScript (Calling a Java Method from Handwritten JavaScript)

public MyUtilityClass
{
    public static int computeLoanInterest(int amt, float interestRate, 
                                          int term) { ... }
    public static native void exportStaticMethod() /*-{
       $wnd.computeLoanInterest =
          @mypackage.MyUtilityClass::computeLoanInterest(IFI);
    }-*/;
}

Is it possible to do this? I tried several different combinations, declaring the native methods and using this.@ and instance.@ with no success.

Thanks

like image 481
Chinesco Avatar asked Dec 23 '22 08:12

Chinesco


1 Answers

Sure it is possible to do this but you syntax is wrong. I'm typing this without compiling, so I might have some typo's. But this is how I do it. The reason why your approach does not work is that the this variable is not what you would expect.

public MyUtilityClass{    
  public static int computeLoanInterest(int amt, float interestRate, int term)  { ... }    

  public static native void exportStaticMethod() /*-{       
      var _this = this;
      $wnd.computeLoanInterest = function(amt,interestRate,term) {
          [email protected]::computeLoanInterest(IFI)(amt,interestRate,term);    
      };
  }-*/;
}
like image 129
David Nouls Avatar answered May 24 '23 21:05

David Nouls