Here’s a super easy way to trigger a view transition and pass a parameter using gwt-presenter:
... import net.customware.gwt.presenter.client.place.PlaceRequest; ... PlaceRequest placeRequest = new PlaceRequest(ManageListsPresenter.PLACE) .with(ManageListsPresenter.PARAM_TEST, "34") .with(ManageListsPresenter.PARAM_ANOTHER, "hello"); Hyperlink my_lists = new Hyperlink("My Lists", placeRequest.toString());
That’s it! No ClickHandlers needed, and gwt-presenter takes care of all the rest.
The PlaceRequest.with() method is a nice touch to gwt-presenter. It all ends up on the URL, which is important for GWT’s History mechanism, and it’s a clean way to generate the URL. Of course, there are corresponding getParameter methods to extract passed parameters from a PlaceRequest, which you would typically do in the presenter’s onPlaceRequest() method.
So what happens when the user clicks the link? The view associated with the requested presenter is shown, the place is marked in GWT’s History mechanism, and the presenter’s onPlaceRequest() method is invoked. No ClickHandlers on the Hyperlink are required to make this happen. When the user clicks a Hyperlink, GWT fires a ValueChangedEvent on History, which is picked up by gwt-presenter and sets off the chain reaction. Even if the requested presenter belongs to a container (WidgetContainerPresenter) that is not currently shown, gwt-presenter calls revealDisplay() on the presenter and its container if needed to bring it to the front.
If you want to use a Button, Label, etc., instead of Hyperlink, you can simply add a ClickHandler and fire the same PlaceRequestEvent that gwt-presenter uses under the covers in response to a Hyperlink:
display.getAddButton().addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { PlaceRequest placeRequest = new PlaceRequest(AddPrayerListPresenter.PLACE); eventBus.fireEvent(new PlaceRequestEvent(placeRequest)); } });
Note: the browser only updates the URL in the address bar in response to a Hyperlink. If you use a Button or other ClickHandler and want to update History and the URL in the address bar, you must also fire a PlaceChangedEvent(), typically in the onPlaceRequest() method that gets called by the original PlaceRequestEvent(). This results in a call to GWT’s History.newItem().
@Override protected void onPlaceRequest(PlaceRequest request) { // necessary when we've fired PRE from code vs Hyperlink eventBus.fireEvent(new PlaceChangedEvent(request)); }
Long-time Web developers will of course recognize that the basic Hyperlink as created above is the same as we’ve been doing for 15 years now. The beauty of gwt-presenter is that with the same simple URL construction, you can easily target one section of the page for dynamic update, and do it in a way that works with browser history and bookmarks to boot. There might be something to this GWT thing.