找出GWT模块何时加载

时间:2010-11-17 23:59:16

标签: gwt jsni

我正在以下列方式将GWT方法导出到本机javascript:

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

在主机页面中,我试图调用它:

<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>

不幸的是,当调用document.ready回调时,尚未定义该函数。从Firebug控制台手动执行时,该功能可以正常工作。

我可以每50毫秒执行一次轮询,直到找到一个名称定义的函数,但这似乎是一种可怕的方法。

如何在加载模块时获得通知,因此当功能可用时?

1 个答案:

答案 0 :(得分:12)

我会尝试在主页中定义一个回调函数,并在onModuleLoad()方法结束时从GWT调用它。

主页功能:

<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.
  DeferredCommand.addCommand(new Command() {
    public void execute() {
      notifyHostpage();
    }
}

private native void notifyHostpage() /*-{
  $wnd.onGwtReady();
}-*/
相关问题