简体   繁体   中英

GWT MVP without tracking History / ignoring back button

First off, before the flames start, I do know that trying to hinder the back button in the browser is a dumb idea. I would not try to do this, but my business partners are very insistent on it. We are porting an existing .exe to the web and their definition of this is 'run it in a browser' and not "make it a web site". So, fact that it's a bad idea (I agree), here's the question:

Is there a way to ignore or trick the GWT PlaceController / History manager mechanisms so that when the back button is pressed, it just stays on the same page? I have used Window.addWindowClosingHandler to add a handler which will prompt the user if they want to leave the page and overriden the newItem() method of the defaultHistorian so that no history is tracked at all, but this isn't quite what the business people want.

What they'd like is to just ignore the back button so that nothing happens when it is clicked. If anyone knows how to do this with GWT, I'd be very grateful.

And I"ve done a lot of google searching and haven't found anything exactly like this question. At least, not close enough to help.

I was able to get GWT to not accumulate any history so that when the user presses the BACK button, they cause an onWindowClosing event to happen and the Browser will prompt them if they want to stay or leave. This will accomplish the goal of not allowing the BACK button to take them back, but it's a bit Draconian. This code does that:

class TvHistorian extends PlaceHistoryHandler.DefaultHistorian
{
  @Override
  public void newItem(String token, boolean issueEvent) {
    // don't do anything - this will prevent history from accumulating
  }
}

final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper, new TvHistorian());

I've tried a bunch of stuff including extending the PlaceController.goTo() to save the "lastNormalFlowPlace". Then, I tried to override the History.onValueChange to use this saved Place if it was different than what the event specified. But I think I missed some subtlety because that didn't work as expected.

With the above exception, my code looks almost exactly like what is documented here: http://www.gwtproject.org/doc/latest/DevGuideMvpActivitiesAndPlaces.html#Putting_it_all_together

I have already posted an answer in the same context.

Please have a look at below links:

Try with any option:

  • History.addValueChangeHandler
  • WindowClosingHandler
  • Event.addNativePreviewHandler
  • $wnd.onbeforeunload

--EDIT--

Sample code: ( It's working perfectly fine in Firefox, Chrome as well as IE9 )

Note: add below code in the beginning of the onModuleLoad() method.

    final String initToken = "Place";

    History.addValueChangeHandler(new ValueChangeHandler<String>() {

        @Override
        public void onValueChange(ValueChangeEvent<String> event) {
            String token = event.getValue();
            if (!initToken.equalsIgnoreCase(token)) {
                History.newItem(initToken);
            }
        }
    });

    // fire the initial history state.
    History.fireCurrentHistoryState();

Note: add checks for other allowed history tokens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM