»
December 29, 2009
»

GWT History Management (Idea)

Still thinking about API. But here is sneak peak of it:

public static class ShowSectionRoute implements Route {

  public Route getParent() {
    return null;
  }

  public String getPath() {
    return "/{section}";
  }

  public void handle(final HandleContext ctx) {
    ctx.register(ShowSectionEvent.getType(), new ShowSectionHandler() {
      public void onShowSection(ShowSectionEvent event) {
        ctx.param("section", event.getSection().name().toLowerCase());
      }
    });
  }

  public void perform(final PerformContext ctx) {
    final Section section = Section.valueOf(ctx.param("section").toUpperCase());
    ctx.register(SectionShownEvent.getType(), new SectionShownHandler() {
      public void onSectionShown(SectionShownEvent event) {
        if (event.getSection() == section) {

          // invokes ShowPersonRoute as it is child route for this one
          ctx.next();
        }
      }
    });
    ctx.fire(new ShowSectionHistoryEvent(section));
  }

}
public static class ShowPersonRoute implements Route {

  private ShowSectionRoute parent;

  public ShowPersonRoute(ShowSectionRoute parent) {
    this.parent = parent;
  }

  public Route getParent() {
    return parent;
  }

  public String getPath() {
    return "/show/{key}";
  }

  public void handle(final HandleContext ctx) {
    ctx.register(ShowPersonEvent.getType(), new ShowPersonHandler() {
      public void onShowPerson(ShowPersonEvent event) {
        ctx.param("key", event.getKey()).run();
      }
    });
  }

  public void perform(final PerformContext ctx) {
    String key = ctx.param("key");
    ctx.fire(new ShowPersonHistoryEvent(key));
  }

}
// Some presenter fires event:
eventBus.fireEvent(new ShowPersonEvent(personKeyAsString));

// ShowPersonRoute handles it and updates route
// History.addItem with "/people/show/<personKey>" is set
// ShowPerson.getParent() route perform() is called what fires ShowSectionHistoryEvent after 
// section is visible (it can be behind GWT.runAsync) ctx.next() is called. 
// It invokes ShowPerson#handle() what fires ShowPersonHistoryEvent with the same person key

// some persenter handles event:
public void onShowPersonHistoryEvent(ShowPersonHistoryEvent e) {
  String key = e.getKey();
}

More on this soon.

 
Internet Explorer 6
Are you serious?