I spoke with the GWT team at Google I/O and Miguel said this question
would be better answered here (by - I think - Joel).

My company is very sensitive about SEO so we usually use GWT to add
behavior to existing elements. But we often run into trouble trying to
listen to events on existing HTML elements.

In short: What's the recommended way to listen for events on
dom.client.Element objects? There seems to be two options:

1. Wrap it in the corresponding widget and use the widget's
addListener methods.

DivElement element;
Label.wrap(element).addClickListener(...);

This seems to work well when you can find a corresponding widget but
some things like HeadingElement don't seem to have any corresponding
widgets. This means that if we want to listen to events on a <h1> tag
we have to first wrap it in a div (with it's own id) which introduces
a new complications.

2. Cast the dom.client.Element to user.client.Element and use the
event listener methods on DOM.

DOM.sinkEvent((com.google.gwt.user.client.Element) element.cast(),
Event.blah);
DOM.setEventListener((com.google.gwt.user.client.Element) element.cast
(),new EventListener(){blah});

This will work for any dom.client.Element but is ugly and leaks memory
in IE6 if you don't unregister your listeners when you're done.

The company I work for started with 1 but have switched to 2 since
it's more generic. However it strikes me as odd that we have to go
through such contortions to perform an operation so basic to
javascript. Is there a option we're not seeing?

If these really are the only options it seems to me that at the very
least the two event methods on DOM should have their argument's scope
expanded to accept the newer dom.client.Element objects.

Besides this issue GWT has been a wonderful tool. Thanks for the help!

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to