简体   繁体   English

如何通过GWT中的历史来确定我是“前进”还是“后退”?

[英]How do I determine whether I am going “forward” or “backward” through my History in GWT?

I am looking at History and History JavaDocs in GWT and I notice that there is no way to tell whether the forward or backward button was pressed (either pragmatically or by the user). 我正在看GWT中的历史历史JavaDocs ,我注意到没有办法判断是按下了向前还是向后按钮(是实用的还是由用户按下的)。 The "button press" is handled by your registered addValueChangeHandler, but the only thing passed to the handler is a string on your history stack. “按下按钮”由您注册的addValueChangeHandler处理,但传递给处理程序的唯一内容是历史堆栈上的字符串。 There is no indication as to whether the "History" is moving "back" (using the back arrow button) or "forward" (using the right arrow button). 没有迹象表明“历史”是“后退”(使用后退箭头按钮)还是“前进”(使用右箭头按钮)。 Is there any way to determine this? 有什么方法可以确定吗?

Sorry, you can't. 对不起,你不能。 And even if you could, there are browsers, like Firefox, that let the user "jump" back more than one page. 即使你可以,也有像Firefox这样的浏览器让用户“跳回”多个页面。 So if you try to relying on relative "coordinates" instead of absolute, the navigation could break your app. 因此,如果您尝试依赖相对“坐标”而不是绝对,导航可能会破坏您的应用程序。

You can always append some kind of counter on your history token. 您可以随时在历史记录标记上附加某种计数器。 It would not be hard, if you have only one history listener. 如果你只有一个历史监听器,那就不难了。

What you can do is have one UrlManager like this: 你可以做的是有一个像这样的UrlManager:

   public class UrlManager implements ValueChangeHandler<String> {

   public UrlManager() {
     History.addValueChangeHandler(this);       
    }

  public void onValueChange(ValueChangeEvent<String> event) {     
    String historyToken = event.getValue();

  }
   }

Save every history token to a list. 将每个历史记录标记保存到列表中。
If it is a completely new history token then add it to the list. 如果它是一个全新的历史记录标记,则将其添加到列表中。 If it is the previous history token, then the back button has been pressed. 如果是以前的历史记录标记,则按下后退按钮。

I suggest using a list to save the tokens and save an iterator that can move up and down the list. 我建议使用一个列表来保存令牌并保存一个可以在列表中上下移动的迭代器。 So initially it points at the end of the list. 所以最初它指向列表的末尾。 If the new token is the previous item on the list, then the back button has been pressed. 如果新标记是列表中的上一个项目,则按下后退按钮。 If you're in the middle of the list (back a few times) and a new entry comes in (new link pressed), remove the rest of the list (can't go forward there anymore) and add the new one- and move iterator to that item. 如果你在列表的中间(返回几次)并且有一个新条目(按下新链接),删除列表的其余部分(不能再向前)并添加新的 - 和将迭代器移动到该项目。

You get the idea. 你明白了。

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

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