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

一世执手 提交于 2019-11-28 01:20:19

问题


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). The "button press" is handled by your registered addValueChangeHandler, but the only thing passed to the handler is a string on your history stack. 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?


回答1:


Sorry, you can't. And even if you could, there are browsers, like Firefox, that let the user "jump" back more than one page. 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.




回答2:


What you can do is have one UrlManager like this:

   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.



来源:https://stackoverflow.com/questions/2693527/how-do-i-determine-whether-i-am-going-forward-or-backward-through-my-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!