Jean-Michel Garcia
Jean-Michel Garcia

Reputation: 2389

GWT manipulating DOM elements caveats

Following this question I have recently asked : Understanding Document.createElement()

Here is the context :

I was wondering :

  1. Is it a bad approach to manipulate DOM elements directly in GWT ? (Without using Widgets)
  2. Is it a bad approach to do things like that Add listener to SpanElement ? Can it cause memory leaks ?
  3. What is the best approach to achieve such things ? I've done some tests with a simple custom widget that uses a span element, and adding 1000 widgets to the RootPanel takes approximatively 6 to 10 seconds in DevMode. When I use DOM elements direclty, this operation duration goes under 1 second (even less than 200ms with optimizations).

EDIT

Performance should not be a problem, according to some real tests I did after @Gilberto advices. http://jmichelgarciagwt.appspot.com/DOMTesting.html

Still, I would love to have feedbacks for questions 1) and 2)

Upvotes: 1

Views: 492

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41099

Adding listeners/handlers to hundreds of span elements/widgets is definitely a bad approach.

If you stay with GWT, you can attach a single event handler to your "text zone" widget, and then find which element has been the source of the click: http://comments.gmane.org/gmane.org.google.gwt/61911

If you go with DOM elements, you can attach a single event listener to your "text zone" element and find out the event source when it bubbles to it. For example: http://icant.co.uk/sandbox/eventdelegation/

Upvotes: 3

Related Questions