本文介绍了共享除前缀之外的所有位置或如何使用PlaceHistoryMapperWithFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的gwt-app中,我有一些除了前缀之外共享所有内容的地方(比如editUserPlace和showUserPlace - 在这种情况下,状态由userId决定)
我目前的尝试是扩展一个摘要UserPlace通过ShowUserPlace和EditUserPlace它们只有一行不同:@Prefix(showUser)/ @Prefix(editUser) - 洞标记器代码必须被复制(我不能继承标记器代码,但覆盖前缀)。

In my gwt-app i have some places that share all but the prefix (like "editUserPlace" and "showUserPlace" - the state is determined by userId in this case)My current attempt is to extend an abstract "UserPlace" by "ShowUserPlace" and "EditUserPlace" they differ only in one line: @Prefix("showUser")/ @Prefix("editUser") - the hole tokenizer code must be copied (i can not inherit the tokenizers code but override the prefix).

位于托马斯建议使用PlaceHistoryMapperWithFactory,但我坚持使用它。

in https://groups.google.com/d/topic/google-web-toolkit/pghMLX27Y4Y/discussion thomas suggested to use "PlaceHistoryMapperWithFactory" but i am stuck with it.

我是否必须为每个地方/标记器提供一个方法(也适用于正常的地方 - 提供他们自己的标记器)?
我是否必须将我的Abstract和/或扩展类广告到@WithTokenizer?
我应该怎样调用setFactory?

Do i have to provide a method for each place/tokenizer (also for places that are "normal" - provideing their own tokenizers)?Do i have to ad my Abstract and/or the extending classes to @WithTokenizer?How/where should i call setFactory?

有没有人使用过PlaceHistoryMapperWithFactory(可能是类似的用例)?并提供一些建议?
是否有人遇到相同的问题并以另一种方式解决它?

Does anybody used PlaceHistoryMapperWithFactory (probably in similar use case)? And giv some advice?Does anybody faced the same issue and solved it another way?

推荐答案

该工厂应该可以工作:


That factory should work:

class MyFactory {
   @Prefix("showUser")
   public PlaceTokenizer<ShowUserPlace> showUserPlace() {
      return new UserPlaceTokenizer<ShowUserPlace>() {
         protected ShowUserPlace createPlace(String id) {
            return new ShowUserPlace(id);
         }
      };
   }

   @Prefix("showUser")
   public PlaceTokenizer<EditUserPlace> showUserPlace() {
      return new UserPlaceTokenizer<EditUserPlace>() {
         protected EditUserPlace createPlace(String id) {
            return new EditUserPlace(id);
         }
      };
   }
}

abstract class UserPlaceTokenizer<P extends UserPlace> implements PlaceTokenizer<P> {
   public P getPlace(String token) {
      // shared logic between both places: parses ID (or whatever) from token
      return createPlace(id);
   }
   public String getToken(P place) {
      // shared logic between both places: build token out of place
      return token;
   }
   protected abstract P createPlace(String id);
}

当然你也可以注入某种 Provider< ; P> 在标记器中,而不是子类化它来覆盖它的 createPlace 方法。

Of course you could also inject some sort of Provider<P> in the tokenizer instead of subclassing it to override its createPlace method.

您可以将它与 @WithTokenizers 一起使用,如果发现两个标记器完全相同的位置或前缀,生成器将会窒息。

You can use it along with @WithTokenizers, the generator will choke if it ever finds two tokenizers for the exact same place or prefix.

您应该在 GWT.create()您的映射器之后调用 setFactory (实际上,问题是你在调用映射器的 getPlace getToken 方法之前设置了工厂。)

You should call setFactory just after you GWT.create() your mapper (actually, what matters is you set the factory before any call to the mapper's getPlace or getToken methods).

这篇关于共享除前缀之外的所有位置或如何使用PlaceHistoryMapperWithFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:40