Since I last checked, you aren’t able to have more than one GWT module on a page.  However, each widget is independent, so it makes a lot of sense to package them up as separate modules.  Then, you have one module that inherits from all of the other ones, and therefore all the widget code gets executed.  Except, sometimes it makes sense for a widget to be on one page and not another.  The easies way I have found to do this is to use a span tag with a unique id to mark a page for a component.  Sometimes the span tag is where the component places itself–other times it just serves as a
marker for a component which manipulates the DOM in other ways.

The code often looks like this:

RootPanel rp = RootPanel.get(SPAN_ID);
if (rp != null) {

MyComponent obj = new MyComponent();
rp.add(obj);
}

Now, if we’re on a page that this component does not make sense on, it never gets intialized, no network calls are made, etc.

One issue is that if you want to have the component in more than one place on the page, you need to handle that with special cases.  You can simply use the same id twice, because it is bad form to have more than one element with the same id on a single page–ids are supposed to be globally unique on a page.  If you could use a special tag, that’d be nice, but GWT does not support getElementsByTagName.

2 thoughts on “GWT Mini Pattern: Use a span to enable

  1. Jason Thrasher says:

    Hi Dan, I’m working on having one GWT module render a Composite on multiple places on a page. You likely wrote this post before it was available, but with GWT 2.0 you can getElementsByTagName:

    Element body = RootPanel.getBodyElement();
    NodeList list = body.getElementsByTagName(“div”);

    You can then iterate the list of elements. I do this with s, and add my configuration for each placement as attributes to the div.

    I’m curious if you’ve found a better strategy?

  2. moore says:

    Hi Jason,

    I like my configuration to be changable without recompiling GWT. Here’s a list of all the runtime configuration methods I have found: http://www.mooreds.com/wordpress/archives/000450

    Now, if you are using getElementsByTagName() to find the appropriate places in the DOM to *attach* to, it sounds like a great idea to me to use that. When I’ve wanted to attach to more than one place, I’ve ended up using code like this

    String spanID = “ATTACH_HERE:100”;

    if (spanID.indexOf(“:”) > 0) {
    String spanPrefix = spanID.replaceAll(“:.*”, “”);
    String spanCount = spanID.replaceAll(“.*:”, “”);
    int count;

    try {
    count = Integer.parseInt(spanCount);
    } catch (NumberFormatException nfe) {
    count = 1;
    }
    for (int j = 0; j < count; j++) { //attach to spanPrefix+j } } And obviously your method is far superior to that. Did I understand your question? Dan PS Err, above, "You can simply use the same id twice" should be "You *can't* simply use the same id twice". IE freaks out, in my experience, if you do so.

Comments are closed.


© Moore Consulting, 2003-2017 +