2 Replies Latest reply on Feb 26, 2013 10:56 AM by mdhirsch30345

    Knowing when a widget is present in the DOM

    mdhirsch30345

      I am trying to create a wrapper for SlickGrid (https://github.com/mleibman/SlickGrid/wiki) which is a pretty nice javascript table or spreadsheet widget.  What I have looks good and is quite featureful, but I'm having trouble instantiating it.

       

      If I try to load slickgrid in the @PostConstruct, it doesn't (usually) work.  The problem is that slickgrid looks for its parent element in the dom at creation time.  Apparently, during the postconstruct the parent is not yet in the DOM.  My parent is a Composite that contains a ScrollPanel, which will be the parent of the slickgrid.  The problem is that I don't know when the ScrollPanel is actually in the DOM so that slickgrid can load.

       

      Here is the code that instantiates the grid:

      <verbatim>

          public void createGrid() {

              if (grid != null) return;

             

              String parentId = parent.getElement().getId();

              boolean parentIsPresent = DOM.getElementById(parentId) != null;

             

              if (parentIsPresent) {

                  Columns gridColumns = new Columns();

                  for (FormattedColumn<R> col : columns) {

                      Column gridColumn = Column.create(col.getId(), col.getName());

                      gridColumns.addColumn(gridColumn);

                  }

                 

                  GridData data = makeData(rows);

                 

                  grid = Grid.create(parentId, gridColumns, data, options);

              }

          }

      </verbatim>

      As you can see, I can check for whether the parent is present in the DOM by querying the DOM.  What I don't know how to do is wait for the parent to be present.  I'm guessing that I need some kind of callback, or the ability to run something when the event loop is idle, but I really don't know.

       

      Is there a way to for the DOM to be complete before running my code?

       

      Thanks,

       

      Michael

        • 1. Re: Knowing when a widget is present in the DOM
          cbrock

          If it's jQuery-based, then you can just use JSNI to execute a callback function in GWT once it's done.

           

           

          private native static void onReady(final Runnable runnable) /*-{
              $wnd.$(runnable.@java.lang.Runnable::run()());
            }-*/;
          
          

           

          Then you just pass a Runnable callback to anything you want done after jQuery has done its thing.

          1 of 1 people found this helpful
          • 2. Re: Knowing when a widget is present in the DOM
            mdhirsch30345

            Thanks Mike, that looks useful.

             

            What I found (almost immediately after posting :-( ) is that I can override onLoad() and get called after the widget is in the document.  But the onReady() solution might be a better fit for jQuery, and I believe slickgrid is jQuery based.

             

            Michael