Thursday, October 2, 2008

onAttach vs onLoad

Always getting these confused...

onAttach() is invoked when the widget is attached to the document. It can be called in the following situations:
  • The widget's parent (which implements HasWidgets) is calling it's own doAttachChildren()
  • The widget's parent is being set (e.g., setParent())
The last thing that onAttach() does is to invoke onLoad().

By default, onLoad() does nothing. So, this is the best place to insert code that has to be run whenever the widget is actually hooked up to the document.

Personally, I think this might be less confusing if the names were chosen better. Perhaps, onAttach() should have been private, and the first thing it could do is call beforeAttach(), and the last thing would be to call afterAttach(). 

Oh well.

P.S.: My suggestion wouldn't quite work because of Composite...it doesn't want/need the base behavior. See, it's not simple...

No comments: