找出 GWT 模块何时加载 [英] Finding out when a GWT module has loaded

查看:16
本文介绍了找出 GWT 模块何时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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.

我可以每 50 毫秒执行一次轮询,直到找到具有该名称的已定义函数为止,但这似乎是一种可怕的方法.

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?

推荐答案

我会尝试在主机页面中定义一个回调函数,并在 onModuleLoad() 方法的末尾从 GWT 中调用它.

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

主页功能:

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

这篇关于找出 GWT 模块何时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆