Search code examples
gwtpgwt-platform

Trying to use PlaceRequest the right way


i have two Presenters: A DevicePresenter and a ContainerPresenter. I place a PlaceRequest in the DevicePresenter to call the ContainerPresenter with some parameters like this:

PlaceRequest request = new PlaceRequest.Builder()
                        .nameToken("containersPage")
                        .with("action","editContainer")
                        .with("containerEditId", selectedContainerDto.getUuid().toString())
                        .build();
placeManager.revealPlace(request);

In my ContainersPresenter i have this overridden method:

@Override
public void prepareFromRequest(PlaceRequest placeRequest) {
    Log.debug("prepareFromRequest in ContainersPresenter");
    super.prepareFromRequest(placeRequest);

    String actionString = placeRequest.getParameter("action", "");
    String id;
    //TODO: Should we change that to really retrieve the object from the server? Or should we introduce a model that keeps all values and inject that into all presenters?
    if (actionString.equals("editContainer")) {
        try {
            id = placeRequest.getParameter("id", null);
            for(ContainerDto cont : containerList) {
                Log.debug("Compare " + id + " with " + cont.getUuid());
                if(id.equals(cont.getUuid())) {
                    containerDialog.setCurrentContainerDTO(new ContainerDto());
                    addToPopupSlot(containerDialog);
                    break;
                }
            }
        } catch (NumberFormatException e) {
            Log.debug("id cannot be retrieved from URL");
        }
    }
}

But when revealPlace is called, the URL in the browser stays the same and the default presenter (Home) is shown instead. When i print the request, it seems to be fine:

PlaceRequest(nameToken=containersPage, params={action=editContainer, containerEditId=8fa5f730-fe0f-11e3-a3ac-0800200c9a66})

And my NameTokens are like this:

public class NameTokens {
    public static final String homePage = "!homePage";
    public static final String containersPage = "!containersPage";
    public static final String devicesPage = "!devicesPage";

    public static String getHomePage() {
        return homePage;
    }

    public static String getDevicesPage() {
        return devicesPage;
    }

    public static String getContainersPage() {
        return containersPage;
    }

}

What did i miss? Thanks!


Solution

  • In your original code, when constructing your PlaceRequest, you forgot the '!' at the beginning of your nametoken.

    .nameToken("containersPage")
    

    while your NameTokens entry is

    public static final String containersPage = "!containersPage";
    

    As you noted, referencing the constant in NameTokens is less prone to such easy mistakes to make!