Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out when a GWT module has loaded

Tags:

gwt

jsni

I am exporting a GWT method to native javascript in the following manner:

public class FaceBookGalleryEntryPoint implements EntryPoint {

    @Override
    public void onModuleLoad() {

        FacebookGallery facebookGallery = new FacebookGallery();
        RootPanel.get().add(facebookGallery);

        initLoadGallery(facebookGallery);
    }

    private native void initLoadGallery(FacebookGallery pl) /*-{
        $wnd.loadGallery = function (galleryId) {
            [email protected]::loadGallery(Ljava/lang/String;)(galleryId);
        };
    }-*/;
}

In the host page, I am trying to invoke it:

<html>
    <head>
        <title>Facebook image gallery</title>
        <script type="text/javascript"
            src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>     
    </head>

    <body>
        <script type="text/javascript" src="/fbg/fbg.nocache.js"></script>
        <h1>Facebook gallery test</h1>
        <script type="text/javascript">
            $(document).ready(function() {
                loadGallery('blargh');              
            });
        </script>
    </body>
</html>

Unfortunately, when the document.ready callback is invoked, the function is not yet defined. When manually executed from the Firebug console the function works just fine.

I could perform some polling every 50 milliseconds until I find a defined function by that name, but it seems like a horrible approach.

How can I get notified when the module is loaded and therefore when the function is available?

like image 997
Robert Munteanu Avatar asked Nov 17 '10 23:11

Robert Munteanu


1 Answers

I would try to define a callback function in the hostpage and call it from GWT at the end of the onModuleLoad() method.

Hostpage function:

<script type="text/javascript">
  function onGwtReady() {
    loadGallery('blargh');              
  };
</script>

GWT:

public void onModuleLoad() {
  FacebookGallery facebookGallery = new FacebookGallery();
  RootPanel.get().add(facebookGallery);

  initLoadGallery(facebookGallery);

  // Using a deferred command ensures that notifyHostpage() is called after
  // GWT initialisation is finished.
  Scheduler.get().scheduleDeferred(new Command() {
    public void execute() {
      notifyHostpage();
    }
  }
}

private native void notifyHostpage() /*-{
  $wnd.onGwtReady();
}-*/;
like image 191
vanje Avatar answered Nov 03 '22 20:11

vanje