开发者

Finding out when a GWT module has loaded

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) {
            pl.@com.example.fbg.client.FacebookGallery::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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜