Domanda

My Gin-injected GWT application uses RequestFactory together with Activities and Places.

To tokenize RequestFactory's stableId() inside Places, I inject the RequestFactory. That gives the class access to getProxyId(token) and getHistoryToken(stableId). The Tokenizer calls non-static, protected methods to tokenize a particular Place.

Here is a sample class.

public class StableIdPlace extends Place {

  @Inject private static Provider<StableIdPlace> provider;

  private final MyRequestFactory requestFactory;
  private EntityProxyId<StableIdProxy> stableId;

  @Inject
  public StableIdPlace(MyRequestFactory rf) {
    requestFactory = rf;
  }

  public void setStableId(EntityProxyId<StableIdProxy> which) {
    stableId = which;
  }

  public EntityProxyId<StableIdProxy> getStableId() {
    return stableId;
  }

  protected void setFromHistoryToken(String token) {
    stableId = requestFactory.getProxyId(token);
  }

  protected String getHistoryToken() {
    return requestFactory.getHistoryToken(stableId);
  }

  public static class Tokenizer implements PlaceTokenizer<StableIdPlace> {
    @Override
    public String getToken(StableIdPlace place) {
      return place.getHistoryToken();
    }
    @Override
    public StableIdPlace getPlace(String token) {
      StableIdPlace place = provider.get();
      place.setFromHistoryToken(token);
      return place;
  }
}

The strategy requires injecting a Provider wherever a new Place is needed. Unfortunately, that includes the static Tokenizers.

I tried to initiate the static injection (for each Place class) with requestStaticInjection() in the Gin client module. What I have works—except for History. I cannot get the static Provider to initialize. It causes a Null exception in the Tokenizer.

Is there a better way to do this? Thank you for your pointers.

È stato utile?

Soluzione

You can use a PlaceHistoryMapperWithFactory to create tokenizer instances out of a Ginjector, Provider or AssistedInject factory.

(I'd rather inject the RF into the tokenizer than the place, the place is about the proxy, the tokenizer about the history token)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top