Philippe Beaudoin
Philippe Beaudoin

Reputation: 3310

In GWT, how to reset the URL when the user hits "Cancel" in the navigation confirmation dialog?

In my GWT application, I want to ask a user confirmation when he navigates out of the current application, i.e. by entering a URL or closing the browser. This is typically done by registering a ClosingHandler and setting the desired dialog message in the onWindowClosing method. This seems to work well.

However, if the user tries to navigate say to http://www.gmail.com (by typing it in the URL bar) and hits Cancel to indicate he doesn't want to navigate, then my app keeps running but the browser's URL bar keeps indicating http://www.gmail.com. This causes a number of problems later in my application and will give the wrong result if the user bookmarks the page.

Is there a way to automatically reset the URL when the user presses Cancel?

Or, alternatively, is there a way to detect the user pressed the Cancel button? If so, is there a way to set the URL without triggering a ValueChangeEvent? (I could add some logic to prevent this, but I'd rather use a built-in mechanism if it exists.)

Upvotes: 4

Views: 993

Answers (3)

Björn
Björn

Reputation: 1601

I solved this by creating a custom PlaceController and replacing the token in the url. Not an ideal solution but it works!

if (warning == null || Window.confirm(warning)) { where = newPlace; eventBus.fireEvent(new PlaceChangeEvent(newPlace)); currentToken = History.getToken(); } else { // update the url when user clicks cancel in confirm popup. History.replaceItem(currentToken, false); }

Upvotes: 0

Philippe Beaudoin
Philippe Beaudoin

Reputation: 3310

I managed to do this. It looks like GWT DeferredCommand are executed after the confirmation window has been closed. This, combined with Hilbrand's answer above, give me exactly what I want. Here is exactly what I do:

  public final void onWindowClosing(Window.ClosingEvent event) {
    event.setMessage(onLeaveQuestion);
    DeferredCommand.addCommand( new Command() {
      public void execute() {
        Window.Location.replace(currentLocation);
      }
    });
  }

Where currentLocation is obtained by calling Window.Location.getHref() every time the history token changes.

Upvotes: 1

Hilbrand Bouwkamp
Hilbrand Bouwkamp

Reputation: 13519

Not sure if this works but did you try: History.newItem(History.getToken(), false); to reset the URL? It does set the history token without triggering a new history item.

Upvotes: 3

Related Questions