1

Unable to call javamethod from javascript in a GWT java class.

Please find the code snippet from below.

package abc;

public class jsclass extends Composite {

public native boolean getOnlineSchedlueResult() /*-{

    function listener(event) {

    //alert("getOnlineScheduleResult called 2 Outside");
     var data = JSON.parse(event.data);
     if(data.FinderSuccess == true){
        parent.onlineMoveNavigation = [email protected]::onlineMoveNavigation()();
     }
  }

  if (parent.addEventListener){
        //alert("parent getOnlineScheduleResult called 3");
        parent.addEventListener("message", listener, false);
        //alert("getOnlineScheduleResult called 3A");
        parent.postMessage("test", "*");
 } else {
        //alert("getOnlineScheduleResult called 4");
        parent.attachEvent("onmessage", listener);
        parent.postMessage("test", "*");
 }

}-*/;

    public void onlineMoveNavigation(){
        GWT.log("onlineMoveNavigation called");
        presenter.moveNavigationNext();
    }

}
3
  • Why post commented code? Commented Feb 2, 2015 at 13:15
  • 1
    A JSNI comment block begins with the exact token /*-{ and ends with the exact token }-*/. It is not a commented code. Commented Feb 2, 2015 at 14:06
  • What is the purpose of parent.onlineMoveNavigation? Is it supposed to store a function or the result of [email protected]::onlineMoveNavigation()()? I'd wager it's the latter case (since onlineMoveNavigation returns void). In which case you want to drop the second pair of parentheses: parent.onlineMoveNavigation = [email protected]::onlineMoveNavigation(); to actually store a reference to the onlineMoveNavigation method in parent.onlineMoveNavigation. Commented Feb 2, 2015 at 22:36

1 Answer 1

1

The this keyword won't evaluate to your jsclass instance when listener is called by the browser.
You have to bind your jsclass instance to a variable that you can reference from your listener function/closure.

var self = this;

function listener() {
  //alert("getOnlineScheduleResult called 2 Outside");
  var data = JSON.parse(event.data);
  if(data.FinderSuccess == true){
     parent.onlineMoveNavigation = [email protected]::onlineMoveNavigation()();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks thomas. Now it is invoking method onlineMoveNavigation. Appreciate your assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.