Pregunta

Still a bit of a GWT noob here but making progress using Activities and Places as described by Google here.

I understand that a Place's "URL consists of the Place's simple class name (like "HelloPlace") followed by a colon (:) and the token returned by the PlaceTokenizer.

Can I somehow remove the colon when I don't have a token to send?

For example, I'm fine with a URL like this "#editPerson:2" when I need to work with PersonId=2. But what about when I just want to present a blank Person form? In that case I would prefer to use "#addPersonForm" rather than "#addPersonForm:"

Any suggestions (even better code suggestions) would be most appreciated!

¿Fue útil?

Solución

You can provide your own PlaceHistoryMapper (without using the generator) as already suggested by Boris_siroB, or you can do it within a PlaceTokenizer with an empty prefix: with an empty prefix, there won't be a colon, and the tokenizer can do whatever you want. If you totally distinct places, make it a tokenizer of Place, so it's also the "catchall" for getToken. That way you can keep all the advantages of the generation with prefixes, PlaceTokenizers and WithTokenizers (if you want to take advantage of them)

Otros consejos

To take full control of the URL hash (that is to generate your own tokens from Places and map these tokens back to Places) you can implement your own history mapper (a class implementing the PlaceHistoryMapper interface).

public class MyPlaceHistoryMapper implements PlaceHistoryMapper {

   @Override
   public Place getPlace(String token) {
        // parse tokens and create Places here  
   }

   @Override
   public String getToken(Place place) {
        // examine Places and compose tokens here
   }
}

In your entry point class you'd then replace the line:

AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);

with:

PlaceHistoryMapper appHistoryMapper = new MyPlaceHistoryMapper();

That's it. Your URL hashes no longer need to be class name-based or to use the : delimiter.

I'm using a PlaceHistoryMapper decorator named PlaceHistoryMapperWithoutColon.

Usage :

final PlaceHistoryMapper historyMapper0 = GWT
                .create(PlaceHistoryMapperImpl.class);

final PlaceHistoryMapper historyMapper = new PlaceHistoryMapperWithoutColon(historyMapper0);

Decorator source :

public class PlaceHistoryMapperWithoutColon implements PlaceHistoryMapper {

    private static final String COLON = ":";

    private PlaceHistoryMapper placeHistoryMapper;

    public PlaceHistoryMapperWithoutColon(PlaceHistoryMapper placeHistoryMapper) {
        this.placeHistoryMapper = placeHistoryMapper;
    }

    @Override
    public Place getPlace(String token) {
        if (token != null && !token.endsWith(COLON)) {
            token = token.concat(COLON);
        }
        return placeHistoryMapper.getPlace(token);
    }

    @Override
    public String getToken(Place place) {
        String token = placeHistoryMapper.getToken(place);
        if (token != null && token.endsWith(COLON)) {
            token = token.substring(0, token.length() - 1);
        }
        return token;
    }

}

Decorated source example :

@WithTokenizers({ FirstPlace.Tokenizer.class, SecondPlace.Tokenizer.class })
public interface PlaceHistoryMapperImpl extends PlaceHistoryMapper {

}

Place source example :

public final class FirstPlace extends Place {

    @Prefix("first")
    public static class Tokenizer implements PlaceTokenizer<FirstPlace> {

        @Override
        public NetworkInfosPlace getPlace(String token) {
            return new FirstPlace ();
        }

        @Override
        public String getToken(FirstPlace place) {
            return "";
        }

    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top