On Oct 5, 2:30 pm, Brian <
hibr...@gmail.com> wrote:
> Hi,
>
> I'm trying to figure out onAttach() onDetatch() onDetachChildren() to
> ensure I'm not leaking, but I don't quite get it. Is there a
> reference somewhere? Am I best digging through the code to figure out
> the flow? I've read the javadocs, but am having a hard time putting
> it together.
>
> Got a gwt app, server sends me down a block of html (a table which is
> a calendar), gwt client takes the html, and calls setInnerHtml() on a
> widget. Perfect.
>
> The server's html has a <div id="foo"></div> that I want to take over,
> and shove widgets into.
Have a look at HTMLPanel then.
> It'd be nice if I could do:
> RootPanel().get("foo").add(...); // but this asserts
>
> What I've done instead is create a wrapper which subclasses Widget,
> implements HasClickHandlers, and makes onAttach() public.
>
> Then I create my wrapped Anchor, get the div in the server's html and
> append:
>
> Anchor link = new Anchor("click me");
> MyWrapper wrapper = new MyWrapper(link.getElement());
> wrapper.addOnClickHandler(...);
> wrapper.onAttach(); // without this, I can't handle the clicks
>
> HorizontalPanel hp = new HorizontalPanel();
> hp.add(link);
> // add more to hp
>
> DivElement serverElement =
> Document.get().getElementById("foo").cast();
> serverElement.appendChild(hp.getElement());
>
> --- This all works great. I've got my panel in the div, and the link
> works.
>
> I'm just wondering if I need to do more, as I don't really 'get' the
> attach, detach, detachChildren flow.
Not detaching your widgets when you remove them from the document (and/
or on window.unload) will lead to memory leaks in some browsers
(mainly, or maybe even *only*, IE6/7/8).
You'll note that RootPanel, and every widget that has a wrap() static
method, will register itself to be "detached on window close", which
will detach all its children.
But really, what you're trying to do is already there, in HTMLPanel.