【发布时间】:2012-07-11 04:06:12
【问题描述】:
我正在使用 GWT 2.4 和 gwt-platform 0.7 和 gin 1.5.0。
我已经为我的 GWT 应用程序的动态(实时)翻译构建了一个库。因此,当LocaleChangeEvent 被触发时,每个小部件都会收到通知,然后让我的TranslationDictionary 显示新字符串。
这些小部件实际上是这样的:
public class LocaleAwareLabel extends Label implements LocaleChangeEventHandler {
TranslationDictionary dictionary;
String translationToken;
public LocaleAwareLabel(TranslationDictionary dictionary, EventBus eventBus, String translationToken) {
this.dictionary = dictionary;
this.translationToken = translationToken;
eventBus.addHandler(LocaleChangeEvent.TYPE, this);
getCurrentTranslationFromDictionary();
}
public void getCurrentTranslationFromDictionary() {
this.setText(dictionary.getTranslation(translationToken));
}
@Override
public void onLocaleChange(LocaleChangeEvent event) {
getCurrentTranslationFromDictionary();
}
}
如您所见:我不能轻易地将这个小部件与 UiBinder 一起使用,目前我在我的 View 中注入 EventBus 和 TranslationDictionary 并像这样使用 @UiField(provided=true):
@UiField(provided=true)
LocaleAwareLabel myLabel;
@Inject
public MyView(TranslationDictionary dictionary, EventBus eventBus) {
widget = uiBinder.createAndBindUi(this);
myLabel = new LocaleAwareLabel(dictionary, eventBus, "someTranslationToken");
}
我想要的: 使用不带@UiField(provided=true) 的小部件,所以我可以简单地将它们放在ui.xml 中,如下所示:
<custom:LocaleAwareLabel ui:field="myLabel" translationToken="someTranslationToken" />
我知道我可以通过 UiBinder 设置 translationToken 使用:
public void setTranslationToken(String translationToken) {
this.translationToken = translationToken;
}
但是由于EventBus 和TranslationDictionary,我仍然无法使用零参数构造函数。另外我不能在构造函数中调用getCurrentTranslationFromDictionary(),因为translationToken的值当然是在构造函数之后设置的。
如果有人可以提供解决方案会很好,也许有代码示例。
还有 P.S.我是一个完全的注射菜鸟,但据我了解,杜松子酒可能会以某种方式解决我的问题。但我不知道怎么做。
谢谢!
【问题讨论】:
标签: gwt dependency-injection uibinder gwt-gin