문제

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

private native void notifyHostpage() /*-{
  $wnd.onGwtReady();
}-*/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top