In GWT, how to reset the URL when the user clicks “Cancel” in the navigation confirmation dialog?

In my GWT application, I want to ask the user for confirmation when they switch from the current application, i.e. by entering the URL or closing the browser. This is usually done by registering the ClosingHandler and setting the desired dialog message in the onWindowClosing method. This seems to work well.

However, if a user tries to switch from say to http://www.gmail.com (by typing it in the URL bar) and clicking Cancel to indicate that he does not want to navigate, my application continues to work but http://www.gmail.com displayed in the browser URL bar http://www.gmail.com . This causes a number of problems later in my application and will give the wrong result if the user closes the page.

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

Or, conversely, is there a way to detect that the user has clicked Cancel ? If so, is there a way to set the url without running a ValueChangeEvent ? (I could add some logic to prevent this, but I would rather use the built-in mechanism if one exists.)

+4
source share
3 answers

I managed to do this. It seems that the GWT DeferredCommand is executed after closing the confirmation window. This, combined with Gilbrand's answer above, give me exactly what I want. That's what I'm doing:

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

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

+1
source

Not sure if this works, but you tried: History.newItem(History.getToken(), false); before reset url? It sets the story marker without causing a new story item.

+3
source

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); }

0
source

Source: https://habr.com/ru/post/1302778/


All Articles