event bubbling and safe stopPropagation()?

1,333 views
Skip to first unread message

Ed

unread,
May 7, 2009, 5:14:56 PM5/7/09
to Google Web Toolkit
Hellu,

I am trying to get a better understanding of the event bubbling and
canceling an event:
1) Does all gwt events bubble up? even if the parent itself doesn't
support the gwt event? So a Panel should receive the focus event of
contained textbox?

2) Suppose I have panel with a textbox and I am the only one that add
a listener to the textbox. Is it safe and better to call
event.stopPropagation() in the listener in this case?
With safe I mean; does anybody else might need the event, like for
example the KeyUpEvent, that I cancel? (maybe the (root) browser-
display itself .......
Any idea about the performance difference if you have "many" widgets
in case you cancel the event?

-- Ed

Thomas Broyer

unread,
May 7, 2009, 9:08:22 PM5/7/09
to Google Web Toolkit


On 7 mai, 23:14, Ed <post2edb...@hotmail.com> wrote:
> Hellu,
>
> I am trying to get a better understanding of the event bubbling and
> canceling an event:
> 1) Does all gwt events bubble up? even if the parent itself doesn't
> support the gwt event?

http://code.google.com/webtoolkit/doc/1.6/FAQ_UI.html#How_can_I_efficiently_handle_events_from_many_interior_Widgets?
links to PPK's excellent writeup on the subject.
See also http://www.slideshare.net/pp.koch/voices-that-matter-javascript-events

...and the answer is "no", not all DOM events bubble; and AFAICT GWT
doesn't bubble "logical" events.

> So a Panel should receive the focus event of contained textbox?

no, because the focus event doesn't bubble.

> 2)  Suppose I have panel with a textbox and I am the only one that add
> a listener to the textbox. Is it safe and better to call
> event.stopPropagation() in the listener in this case?
> With safe I mean; does anybody else might need the event, like for
> example the KeyUpEvent, that I cancel? (maybe the (root) browser-
> display itself .......

Someone could always use the capture phase (NativePreviewEvent/
NativePreviewHandler) and see the events, so calling stopPropagation
isn't probably worth it.

> Any idea about the  performance difference if you have "many" widgets
> in case you cancel the event?

I do believe bubbling is somehow optimized by browsers, as this is the
"normal" behavior; so I doubt stopPropagation would really make a
difference (though I have no numbers).
More importantly, I think you should only use stopPropagation on
purpose, to make your code easier to understand, thus to maintain (uh,
why did I prevent bubbling of this event 2 months ago? there must have
been a good reason, but which one?).

Ed

unread,
May 8, 2009, 4:53:47 AM5/8/09
to Google Web Toolkit
He Thomas,

Thanks again for the info. Exactly what I was looking for.

Too bad that not all events bubble up so you can always subscribe your
listeners to one of his parents.. This would make it more transparent.

This is espcially usefull when you have a panel with a view and edit
mode.
The view and edit mode both include different widgets but they both
support the focus event.
Now I have to switch the focus listener (wrapper) between the
underlying widgets... or could this be done better?

-- Ed
Reply all
Reply to author
Forward
0 new messages