I know I'm probably just doing something stupid but I cannot find an example that actually shows how to call a Java method from javascript using GWT.
I've followed the documentation almost verbatim where is says:
package mypackage;
public class Account {
private int balance = 0;
public int add(int amt) {
balance += amt;
}
public native void exportAdd() /*-{
var that = this;
$wnd.add = $entry(function(amt) {
[email protected]::add(I)(amt);
});
}-*/;
}
Then you can call it in JS using
$wnd.add(5);
But that results in an error for me that says "$wnd is undefined".
This is my code: I export the function call
public native void exportPaymentProcessComplete()/*-{
var that = this;
console.log('exportingPaymentProcessComplete');
$wnd.paymentProcessComplete = $entry(function(result){
[email protected]::paymentProcessComplete(Ljava/lang/String;)(result);
});
}-*/;
I have a simple function that's being called (with a breakpoint because I've yet to get it to be called)
public void paymentProcessComplete(String result){
if(result != null){
}
}
This is the tricky part and probably where I'm going wrong. The JSNI call is being made from an iframe when it loads. I think it has to do with trying to call the parent window's javascript functions but I'm not sure how to refer to the parents $wnd object.
I've tried this:
response.getWriter().print("<script type=\"text/javascript\">parent.$wnd.paymentProcessComplete(\"SUCCESS\");</script>");
Which is when I get the "$wnd is undefined" error.
And also this:
response.getWriter().print("<script type=\"text/javascript\">parent.paymentProcessComplete(\"SUCCESS\");</script>");
Which gives me a "Unable to get property 'paymentProcessComplete' of undefined or null reference". Which is basically the same error as "$wnd is undefined".
Anyone have any thoughts on how to accomplish this?