简体   繁体   English

jQuery UI无法通过GWT正确加载

[英]jQuery UI won't load properly with GWT

I don't work much with javascript, but I'm trying to get dragging working with the Modal boxes from the GWT Bootstrap project. 我在JavaScript方面工作不多,但是我试图从GWT Bootstrap项目中拖拽使用Modal框。 So I have a GWT project, I have an entrypoint module. 所以我有一个GWT项目,有一个入口点模块。 I add a Modal dialog box to the document and give it a title. 我在文档中添加了“模态”对话框并为其命名。 Then I try to call the draggable method from the jQuery UI framework on that widget. 然后,我尝试从该小部件的jQuery UI框架中调用draggable方法。 Since it's just a div, everything should work. 由于它只是一个div,因此一切正常。

My first problem was that the stuff in the <script> tags was getting run before the element ever got added to the DOM. 我的第一个问题是<script>标记中的内容在元素添加到DOM之前就已经运行。 So I moved it into the notifyHostPage() function and hoped to call it from there. 因此,我将其移入notifyHostPage()函数,并希望从那里调用它。 But jQuery UI seems to unload itself from the page after it exits the tag the first time. 但是,jQuery UI似乎在第一次退出标记后会从页面上卸载自身。 I've included the code to show what I've tried, and the output it results in. This is really crazy. 我已经包含了代码以显示我尝试过的内容以及所产生的输出。这确实很疯狂。

I've tried moving the <scripts> to below the nocache.js file, I've tried just putting them in the gwt.xml, but because of super dev mode or whatever it won't let me. 我尝试将<scripts>移至nocache.js文件下方,我尝试将其放入gwt.xml中,但是由于超级开发模式或其他原因而无法实现。 I've tried using document.write() to add the scripts inside the notifyHostPage() function. 我尝试使用document.write()notifyHostPage()函数中添加脚本。 I've tried just adding the class ui-draggable to the modal dialog, but because jQuery UI disappears that doesn't work either. 我试过只将ui-draggable类添加到模式对话框,但是因为jQuery UI消失了也不起作用。 I've tried using local resources and a CDN. 我尝试使用本地资源和CDN。 I'm kind of at my wits end. 我有点机智。

module.html module.html


<script type="text/javascript" language="javascript" src="module/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.js"></script>
<script type="text/javascript" language="javascript" src="module/module.nocache.js"></script>

<script type="text/javascript">
  $(function() {
    if (jQuery.ui) {
      // UI loaded
      console.log("yes");
      console.log(jQuery.ui);
    } else {
      console.log("no");
    }
  });
  function startDragging() {
    if (jQuery.ui) {
      // UI loaded
      console.log("yes");
      console.log(jQuery.ui);
    } else {
      console.log("no");
    }
    $("#myModal").draggable({
      handle: ".modal-header"
    }); 
  };
</script>

Entrypoint module 入口点模块


  public native void notifyHostPage() /*-{
    if ($wnd.jQuery.ui) {
      // UI loaded
      console.log("yes");
      console.log(jQuery.ui);
    } else {
      console.log("no");
    }
    $wnd.startDragging();
  }-*/;

Output 产量


yes
Object
  accordion: function ( options, element ) {
  autocomplete: function ( options, element ) {
  button: function ( options, element ) {
  buttonset: function ( options, element ) {
  datepicker: Object
  ddmanager: Object
  dialog: function ( options, element ) {
  draggable: function ( options, element ) {
  droppable: function ( options, element ) {
  hasScroll: function ( el, a ) {
  ie: false
  intersect: function (draggable, droppable, toleranceMode) {
  keyCode: Object
  menu: function ( options, element ) {
  mouse: function ( options, element ) {
  plugin: Object
  position: Object
  progressbar: function ( options, element ) {
  resizable: function ( options, element ) {
  selectable: function ( options, element ) {
  slider: function ( options, element ) {
  sortable: function ( options, element ) {
  spinner: function ( options, element ) {
  tabs: function ( options, element ) {
  tooltip: function ( options, element ) {
  version: "1.10.1"
  __proto__: Object
no
no 

Final Solution: 最终解决方案:

I've updated my files as so: 我已经更新了文件,如下所示:

entrypoint.java entrypoint.java


public class RecondoHISModern implements EntryPoint {
  final RecondoHISServletInterfaceAsync recondoHIS = GWT.create(RecondoHISServletInterface.class);
  public void onModuleLoad() {
    loadScripts();
  }
  private void loadScripts() {
    // ScriptInjector.fromString("public/js/jquery-ui-1.10.4.custom.min.js").inject();
    // ScriptInjector.fromString("public/js/nprogress.js").inject();
    List<String> scripts = Arrays.asList( //"//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.js",
                                          "//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js",
                                          "//cdnjs.cloudflare.com/ajax/libs/nprogress/0.1.2/nprogress.min.js");
    injectScriptsInOrder(scripts);
  }

  private void injectScriptsInOrder(final List<String> scripts) {
    if (scripts.size() > 0) {
      ScriptInjector.fromUrl(scripts.get(0))
      .setRemoveTag(false)
      .setWindow(ScriptInjector.TOP_WINDOW)
      .setCallback(new Callback<Void, Exception>() {

        @Override
        public void onFailure(Exception reason) {
          GWT.log("The script " + scripts.get(0) + " did not install correctly");
        }

        @Override
        public void onSuccess(Void result) {
          GWT.log("The script " + scripts.get(0) + " installed correctly");
          injectScriptsInOrder(scripts.subList(1, scripts.size()));
        }
      }).inject();
    } else {
      createModal();
    }
  }

  public void createModal() {
    Modal modal = new Modal();
    modal.setTitle("Java Created Modal");
    modal.setClosable(true);
    ModalBody modalBody = new ModalBody();
    modalBody.add(new Span("Create in Java Code!"));

    ModalFooter modalFooter = new ModalFooter();
    modal.add(modalBody);
    modal.add(modalFooter);

    modal.setId("myModal");
    modal.show();
    draggable();
  }

  private native void draggable() /*-{
    $wnd.jQuery("#myModal").draggable({
      handle: ".modal-header"
    });
  }-*/;
}

At first it was crashing at this function in modal.show(); 起初,它在modal.show()中的该函数处崩溃;

private native void modal(final Element e, final String arg) /*-{
    $wnd.jQuery(e).modal(arg);
}-*/;

But then I realized that maybe I shouldn't be loading jQuery twice, so I removed jQuery from the ScriptInjector list and everything started working fine! 但是后来我意识到也许我不应该两次加载jQuery,所以我从ScriptInjector列表中删除了jQuery,一切开始正常!

You shouldn't load JS dependencies via <script> tags in your HTML file. 您不应通过HTML文件中的<script>标记加载JS依赖项。 Either specify them in your module .gwt.xml file via <script> or better use ScriptInjector for compatibility with SuperDevMode. 通过<script>在模块.gwt.xml文件中指定它们,或者更好地使用ScriptInjector以与SuperDevMode兼容。

Create an EntryPoint for your module and inject the JS dependencies there. 为您的模块创建一个EntryPoint ,然后在其中注入JS依赖项。 See how we did it in our GwtBootstrap3 project. GwtBootstrap3项目中了解我们如何做到的 Since you're using GwtBootstrap3 you don't need to inject jQuery. 由于您使用的是GwtBootstrap3,因此不需要注入jQuery。 This will already be done by GwtBootstrap3. 这已经由GwtBootstrap3完成。 Just inject jQuery UI and make sure to specify your <entry-point> after all <inherits> . 只需注入jQuery UI,并确保在所有<inherits>之后指定您的<entry-point> <inherits>

Once this is resolved you should be able to run something like this from your presenter: 解决此问题后,您应该可以从演示者运行以下内容:

private native void draggable() /*-{
   var $modal = $wnd.jQuery("#myModal");
   $modal.draggable(...);
}-*/;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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