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
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);
};
}-*/;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With