GWT Anchor to Place?
Asked Answered
P

4

5

In a GWT 2.1+ app, how can I generate a link to a place for external consumption?

For instance, say I want to create a link to Place1. For internal consumption I could do presenter.goTo(new Place1("token")). How can I make this into an Anchor or some sort of link that users can paste into their browser?

Purplish answered 8/3, 2011 at 6:56 Comment(0)
B
5

Here's how I would do:

final Place1 place = new Place1("token");
Anchor anchor = new Anchor("go to place 1", "#" + placeHistoryMapper.getToken(place));
anchor.addClickHandler(new ClickHandler() {
  public void onClick(ClickEvent event) {
    placeController.goTo(place);
    event.preventDefault();
  }
});
Burdine answered 12/3, 2011 at 11:8 Comment(2)
Thanks Thomas, but doesn't this still have the issue of not getting the prefix?Purplish
No, the PlaceHistoryMapper is what the PlaceHistoryHandler uses to generate and parse tokens, it is (it has to be!) fully aware of PlaceTokenizer's prefixes.Burdine
D
2

As far as I know as i am new to GWT myself, if you use Hyperlink instead of Anchor you will not have to write the event handler. It will redirect you to the place and handle the history stuff automatically.

Durkheim answered 13/3, 2011 at 16:45 Comment(0)
I
1

You can convert a place to a token string using the PlaceHistoryMapper. See https://developers.google.com/web-toolkit/doc/latest/DevGuideMvpActivitiesAndPlaces for details on how to implement the MVP design in GWT.

final YourImplementationOfPlaceHistoryMapper placeHistoryMapper = GWT.create(YourImplementationOfPlaceHistoryMapper.class);

final Hyperlink link = new Hyperlink("A Link To A Place", placeHistoryMapper.getToken(new YourNewPlace()));
Idiocy answered 12/4, 2012 at 21:59 Comment(1)
Nice solution, because it enables right-click open-in-new-tab.Marenmarena
K
0

If you have already mapped the token to a place, simply create an anchor with the href property equals to the token.

Anchor anchor = new Anchor("go to place1 ", "token");
Killy answered 8/3, 2011 at 8:30 Comment(1)
Wouldn't the href have to be PlacePrefix:Token? How can I programmatically get the PlacePrefix?Purplish

© 2022 - 2024 — McMap. All rights reserved.