Search code examples
javascripthtmlgwtsmartgwt

SmartGWT / GWT accessing div created in constructor via javascript


I am trying to integrate a custom javascript library with my SmartGWT project.

I have areas of my page which are loaded dynamically via button clicks and so in the Java code I have something like the following pseudo code:

button . onClick {
    contextArea = new contextAreaTypeA()
}

So my contextAreaTypeA is created and within the constructor I want to create a series of div elements, assign them IDs and then call a function in my custom library which loads content into those divs.

SmartGWT gets rid of any way of getting hold of IDs or setting IDs on any element in the page, to create IDs on elements you either have to create your own widget and override getInnerHtml or create something like a HTMLFlow and add content like myflow.setContent('<div id="myID" ></div>'); which is the way I am doing it.

My problem is that doing this in the constructor means that when I come to call my custom javascript function which takes an ID, this div I created does not seem to be attached to the dom yet so I always get a null pointer.

Since my module and document are already loaded and this content is being dynamically added in I can't call document ready or onModuleLoad, so how and when should I be detecting the load of my div and when is it safe to access the div from within JSNI calls?

I have tried multiple methods such as adding attach handlers (they never seem to be called), I have tried adding draw handlers to the HTMLFlow but they seem to be called after its drawn the htmlflow div but before it's actually drawn the contents. I have tried calling redraw on the HTMLFlow() but this doesn't even seem to call an onDraw method at all.

I have no idea how to go about integrating this library with my SmartGWT, if anyone could suggest a better (or any) way of doing this that would be great.


Solution

  • So it turns out the problem I thought I had was not a problem at all, in fact it was something entirely different.

    onAttach() still does not fire, for reasons I cannot seem to work out, however doing everything in a drawHandler works fine.

    The reason my reference was coming out null was that when I was trying to access the element within javascript I was using

    document.getElementById("myDiv");
    

    whereas I should have been using

    $wnd.document.getElementById("myDiv");
    

    This is down to the way GWT boxes scripts in iframes I believe (my understanding is so the scripts can be dynamically injected at runtime? but I may be wrong).