views:

37

answers:

2

Hi. Here's my history value change event handler:

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

    if (token != null) {

      if (token.equals("!list")) {
          GWT.runAsync(new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
            }

            public void onSuccess() {
                presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
                presenter.go(container);
            }
        });
      }
      else if (token.equals("!add")) {
          GWT.runAsync(new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
            }

            public void onSuccess() {
                presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
                presenter.go(container);
            }
        });
      }
      else if (token.equals("!edit")) {
          GWT.runAsync(new RunAsyncCallback() {

                public void onFailure(Throwable caught) {
                }

                public void onSuccess() {
                    presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
                    presenter.go(container);
                }
          });
      }

    }

As you can see, going to www.domain.com/#edit loads up the edit view. But, how would I specify a parameter in the fragment, e.g. an id, and pass it on to the Edit Contacts Presenter?

www.domain.com/#edit/1

+1  A: 

First of all, your example looks broken as the add and edit cases do exactly the same thing onSuccess. But I'm sure you already knew that ;-)

I've not used GWT since 1.5, but from memory we did this with string matching, e.g:

if (token.startsWith("edit")) {
  String userID = token.substring("edit".length() + 1);
  //...
}

I'd hope there were helpers in newer versions of GWT as serializing and deserializing bits of your object model to URL-safe tokens to support history was one of the more painful GWTisms.

Martin Hutchinson
Doesn't look like there is, so I just did it using the basic String methods. =)
Matt H
+1  A: 

The token you get via event.getValue() is just a String - so you can use token.split("/") to get all the fragments and then proceed according, for example, to the first one (if we get "edit", then we should expect a number next, etc.).

Igor Klimer
Yup, much simpler than I thought! Thanks!
Matt H