tags:

views:

44

answers:

1

It has been a while since I've touched GWT, but I was recently looking at GWT applications to see how they accomplished certain tasks. I noticed that if you go into AdWords (a GWT application), you can manipulate table information in-line. For example, if I go into my campaign and click the pencil icon next to the ad group, a little popup will appear allowing me to change the ad group's name ... except there's no identifying information anywhere in the DOM structure. No hidden fields, no id's snuck into the div elements.

What's going on here? I've been working with interactive tables, but I've always established a click handler on a class and stuck an ID in there so I can tell what I'm editing. I've always found this as unsatisfactory. Any ideas?

+1  A: 

It uses a JavaScript variable to get a hold on the element directly when it's created. That variable can then be stored somewhere - as long as it's accessible directly or indirectly from the the global object (document), it can be retrieved later from there.

A simple pure JavaScript example would be:

document.myParagraph = document.createElement('p');
document.body.appendChild(myParagraph);

document.mySpan = document.createElement('span');
document.myParagraph.appendChild(mySpan);

...

document.mySpan.onclick = ...
Chris Lercher
Ah, yes! This makes much more sense. I thought that doing this was bad practice (when writing in pure Javascript), am I misinformed or is the fact that this is all managed, compiled javascript that concerns about using global variables like this are simply not there?
chum of chance
@chum: Well, it's just a simple example that should show the principle of how the variables can be accessed. It wouldn't be a good idea to really store each variable directly in the document - but all you really need is one reference (directly or indirectly) to some object, which can then store other objects etc. This is similar to Java's `main` method - you need one global starting point, but the rest doesn't have to be in global variables (and shouldn't be). The starting point can also be an event handler, so you don't necessarily have to start directly at the document.
Chris Lercher