Ajay Babu Kunapareddy
Ajay Babu Kunapareddy

Reputation: 11

GWT and Javascript

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();
    }

}

Upvotes: 1

Views: 52

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

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()();
  }
}

Upvotes: 1

Related Questions