Question

I'm a bit puzzled about the examples I find for GWT's History mechanism.

Looking and running the example GWT History it doesn't seem to be very good.

History is explained often as something that results from Changes in the application.

The example has multiple problems:

  • Initial state is as well the empty token and the token "page0"

    --> "History" Back shows the same page twice before "leaving the application"

  • History and State are synced somehow indirect.

    • A Tab selection causes a selection event, which sets a new token. Settings the token causes a History Value Change, that selects the same tab again (?). That pattern is kind of mindtwisting.

Would it be so wrong to look at the problem from another perspective?

I think it would be much more cleaner to always change the state according to the history token.

In that case ValueChange would implement the "state rendering". A Button that wants to change state wouldn't do it directly but using the History.newItem() method, and change the state indirectly using the valueChange method.

e.g.

public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();
    if(token.isEmpty()){
        show("welcome");
    }else if(token.equals("registration")){
        show(token);
    }
}

And a button that wants to display registration:

welcome = new Button("Registration", new ClickHandler() {           
        @Override
        public void onClick(ClickEvent event) {
            History.newItem("registration");                
        }
    });

This is somehow using history to drive the applications state.

What's wrong about that, why is it done the other way around everywhere?

Was it helpful?

Solution

GWT History does offer a way to set the token without causing the on history changed method to be called History.newItem("registration", false);

As for your suggestion specifically about GWT History alone, ValueChange would implement the "state rendering", that would not work in cases where you need to pass more than a String to set the state. In such a case, I set a token as above and then use MVC to forward whatever data is needed to the view for rendering.

In any case, I definitely do not go through onValueChange in order to select a tab. I use rather something like tabPanel.selectTab(tabNum);

FWIW, The current state of GWT thinking moved from just history to using Activities and Places

You may want to see that as perhaps it better aligns with what you are trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top