gwt hyperlink changeEvent
How do I make a Hyperlink for the current page fire the history change event?
For example, on http://localhost:8080/index.html#foo there is a Hyperlink w开发者_运维技巧ith a historyToken of foo. How do I get the app to process/fire that click/change event?
Am I at the mercy of the nature of browser behavior?
To get your app to respond to the click of the Hyperlink you can add a ClickHandler to the Hyperlink. The ClickHandler should do the work of updating your app interface, etc. The history token will get updated automatically based on the history token set in the constructor of the Hyperlink and the History.newItem() method does not need to be called.
To handle arbitrary URL's with your possible tokens being pasted into url field of the browser or bookmarked by your users you need to implement a ValueChangeHandler. Check the documentation of HyperLink for an example.
Something like this fires this URL and adds it to the History
History.newItem("foo", true);
this just adds it to the History as a response to user actions:
History.newItem("foo", false);
That is install HistoryListener (onHistoryChanged), and react on that one only.
If user clicks a link/goes to favourites directly, respond from within onHistoryChanged.
When user does something and you need to change the URL, fire History.newItem(...,true), and again respond to it within onHistoryChanged.
Hyperlink myLink = new Hyperlink("foo");
myLink.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String currentToken = History.getToken();
String newToken = ((Hyperlink) event.getSource()).getTargetHistoryToken();
if (currentToken == newToken) {
History.fireCurrentHistoryState();
}
}
});
精彩评论