简体   繁体   English

GWT MVP,无需跟踪历史记录/忽略后退按钮

[英]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". 我们正在将现有的.exe移植到Web上,其定义是“在浏览器中运行”而不是“使其成为网站”。 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? 有没有一种方法可以忽略或欺骗GWT PlaceController /历史记录管理器机制,以便当按下后退按钮时,它仅停留在同一页面上? 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. 我已经使用Window.addWindowClosingHandler添加了一个处理程序,该处理程序将提示用户是否要离开页面并覆盖defaultHistorian的newItem()方法,以便根本不会跟踪任何历史记录,但是这与业务无关人们想要。

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. 如果有人知道如何使用GWT做到这一点,我将非常感激。

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. 而且我已经在Google上进行了大量搜索,但没有找到与该问题完全相同的东西。至少,没有足够的帮助。

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. 我能够使GWT不累积任何历史记录,因此当用户按下BACK按钮时,它们将导致发生onWindowClosing事件,并且浏览器将提示他们是否要留下。 This will accomplish the goal of not allowing the BACK button to take them back, but it's a bit Draconian. 这将实现不允许“返回”按钮将其取回的目标,但是有点像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". 我已经尝试了很多方法,包括扩展PlaceController.goTo()以保存“ lastNormalFlowPlace”。 Then, I tried to override the History.onValueChange to use this saved Place if it was different than what the event specified. 然后,我尝试重写History.onValueChange以使用此保存的Place(如果它与指定的事件不同)。 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 除了上述例外,我的代码几乎与此处记录的内容完全一样: 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 ) 示例代码:( 在Firefox,Chrome和IE9中都可以正常工作

Note: add below code in the beginning of the onModuleLoad() method. 注意:在onModuleLoad()方法的开头添加以下代码。

    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. 注意:添加对其他允许的历史标记的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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