简体   繁体   中英

GWT: external javascripts loading delayed?

I am facing a strange situation: the customer provided us some HTML we are attaching GWT on. We are not allowed to use UI Binding, so we have to load the page as is, retrieve elements by their id and change what we need to.

These pages include some third party javascripts, but often at the first load of a page I get some exception about methods not being found. If I reload the page the error is gone and the page works fine. I suspect that when the page loads these JS are not available yet, that's why I get errors. Is there any way to ensure the external libraries are ready before loading xxx.nocache.js?

Another strange issue: when I add GWT to the page, all the listeners (eg button's onClick) are gone and I have to reattach them.

When I open the original page (w/o GWT) everything works fine.

Any suggestions?

Although the load process of scripts in an html document is done sequentially, it does not guarantees that all loaded scripts initialize things in this sequence, because many of them could do things asynchronously like GWT bootstrap does when loading the appropriate permutation.

Said that, you should write some code in GWT so as it waits until all need stuff has been loaded. Use a timer or a scheduler:

  Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {

    native boolean check() /*-{
      // return true when all 3party js properties are available
      return !! $wnd.some_library_property
    }-*/;

    public boolean execute() {
      if (check()) {
        // Call your real entrypoint code here
        return false;
      } else {
        return true;
      }
    }
  }, 100);

Related your second question, when you wrap some elements with gwt widgets sometimes you are going to lose previous attached events depending on the widget. Can you post some code about how are you wrapping these elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM