我正在使用GWTP平台和eClipse来构建webapp。在 Eclipse 中,当创建演示器时,它将创建 3 个文件(例如:SearchPresenter.java、SearchView.java 和 SearchView.ui.xml):
public class SearchView extends ViewImpl implements SearchPresenter.MyView
public class SearchPresenter extends
Presenter<SearchPresenter.MyView, SearchPresenter.MyProxy>{
....
private EventBus eventBus;
@Inject
public SearchPresenter(final EventBus eventBus, final MyView view) {
super(eventBus, view);
this.eventBus=eventBus;
}
}
要使用eventBus,我们只需使用eclipse创建EventBus文件,例如MyEvent.java,然后使用以下代码在SearchPresenter中调用eventBus:
MyEvent mEvent=new MyEvent();
SearchPresenter.this.eventBus.fireEvent(mEvent);
现在假设我有一个非演示器类public class SearchDialogBox extends DialogBox
,那么我的问题是如何在搜索对话框中使用 MyEvent?如何在 SearchDialogBox 中获取 EventBus()?
使用 GWTP,但我想以下内容没问题。
@Inject private EventBus eventBus
应该可以工作(如果您没有立即在搜索对话框构造函数中使用它)。
否则,请尝试找出GWTP中的哪个类扩展了com.google.gwt.inject.client.Ginjector。假设它被称为"MyInjector",只需写:
private EventBus eventBus = MyInjector.INSTANCE.getEventBus();
看看 https://github.com/ArcBees/GWTP/wiki/Events。
你基本上实现了HasHandlers
接口给自己注入了EventBus
。
-
使搜索对话框扩展 BaseEventHandler:
SearchDialogBox extends BaseEventHandler<YOUR_EVENT_BUS>
对于 DialogBox ,请使用组合而不是继承,因为现在您的 SearchDialogBox 扩展了 BaseEventHandler
- 使用@EventHandler批注搜索对话框(如果需要,还可以@Singleton)
在YOUR_EVENT_BUS至少创建一个应在搜索对话框中处理的方法
例如,在YOUR_EVENT_BUS:
@Event(handlers = {SearchDialogBox.class}) void helloWorld();
和在搜索对话框中
`public void onHelloWorld(){...}`