Here's my history value change event handler:
public void onValueChange(ValueChangeEvent<String> event) {
String token = event.getValue();
if (token != null) {
if (token.equals("!list")) {
GWT.runAsync(new RunAsyncCallback() {
public void onFailure(Throwable caught) {
}
public void onSuccess() {
presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
presenter.go(container);
}
});
}
else if (token.equals("!add")) {
GWT.runAsync(new RunAsyncCallback() {
public void onFailure(Throwable caught) {
}
public void onSuccess() {
presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
presenter.go(container);
}
});
}
else if (token.equals("!edit")) {
GWT.runAsync(new RunAsyncCallback() {
public void onFailure(Throwable caught) {
}
public void onSuccess() {
presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
presenter.go(container);
开发者_运维知识库}
});
}
}
As you can see, going to www.domain.com/#edit loads up the edit view. But, how would I specify a parameter in the fragment, e.g. an id, and pass it on to the Edit Contacts Presenter?
www.domain.com/#edit/1
The token you get via event.getValue()
is just a String - so you can use token.split("/")
to get all the fragments and then proceed according, for example, to the first one (if we get "edit", then we should expect a number next, etc.).
First of all, your example looks broken as the add and edit cases do exactly the same thing onSuccess. But I'm sure you already knew that ;-)
I've not used GWT since 1.5, but from memory we did this with string matching, e.g:
if (token.startsWith("edit")) {
String userID = token.substring("edit".length() + 1);
//...
}
I'd hope there were helpers in newer versions of GWT as serializing and deserializing bits of your object model to URL-safe tokens to support history was one of the more painful GWTisms.
精彩评论