Using GWTs activities and places can I navigate to a previous page without using History.back()?

StackOverflow https://stackoverflow.com/questions/7672895

Frage

I am using GWTs Activities and Places pretty much as described on http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html and it is all working fine.

What I would like to do is from a particular page navigate to the previous page without using History.back() as I don't want to lose the history state. (I have a page where the user performs an action, on success I want to return to the previous page and keep the history state, on the other hand if they cancel I do want to use History.back() as I do want to lose the state).

The only way I can think of to do this is to create my own Place/History tracking code that listens to Place/History change events and makes the previous Place available to me so that I can call placeController.goto(...)

Is there an easier way of doing this? Am I missing something?

War es hilfreich?

Lösung

You have to somehow keep track of were to return, because instead of cancel the user can hit the back button, which would be the same as clicking cancel, except there is no code in your application executed, so you have no control.

Secondly if you have the history in the url, the user could navigate directly to that page and then you should know where to go to when the user click ok. Or if the user goes directly to the page, redirect the user to another page.

One approach is to store the return history token in the history token of the page you go to. When the page is finished it can go back(or technically it would be 'go forward') to that page based on the passed return token. (Although with GWT you could easily store the history token in code).

Andere Tipps

The approach I took was to store the history token in code (as suggested). I extended PlaceController and used it to track Place changes on the EventBus. Now everywhere I was using PlaceController I instead use PlaceControllerExt which has a nice previous() method that takes me back to where I came from - but navigates forward and never leaves the application.

public class PlaceControllerExt extends PlaceController {

  private final Place defaultPlace;
  private Place previousPlace;
  private Place currentPlace;

  public PlaceControllerExt(EventBus eventBus, Place defaultPlace) {

      super(eventBus);
      this.defaultPlace = defaultPlace;
      eventBus.addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() {

          public void onPlaceChange(PlaceChangeEvent event) {

              previousPlace = currentPlace;
              currentPlace = event.getNewPlace();
          }
      });
  }

  /**
   * Navigate back to the previous Place. If there is no previous place then
   * goto to default place. If there isn't one of these then it'll go back to
   * the default place as configured when the PlaceHistoryHandler was
   * registered. This is better than using History#back() as that can have the
   * undesired effect of leaving the web app.
   */
  public void previous() {

      if (previousPlace != null) {
          goTo(previousPlace);
      } else {
          goTo(defaultPlace);
      }
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top