1

我正在使用 GWTP 平台和 eClipse 来构建 webapp。在 Eclipse 中,创建 Presenter 时,会创建 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,那么我的问题是如何在 SearchDialogBox 中使用 MyEvent?如何在 SearchDialogBox 中获取事件总线()?

4

3 回答 3

1

我不使用 GWTP,但我想以下是可以的。

@Inject private EventBus eventBus

应该可以工作(如果您不立即在 SearchDialogBox 构造函数中使用它)。

否则尝试找出 GWTP 中的哪个类扩展了 com.google.gwt.inject.client.Ginjector。假设它被称为“MyInjector”,只需编写:

private EventBus eventBus = MyInjector.INSTANCE.getEventBus();
于 2013-10-18T10:16:46.323 回答
0

看看https://github.com/ArcBees/GWTP/wiki/Events

您基本上实现HasHandlers了接口注入自己的EventBus.

于 2013-10-18T11:54:20.283 回答
0
  1. Make your SearchDialogBox to extend BaseEventHandler:

    SearchDialogBox extends BaseEventHandler<YOUR_EVENT_BUS>    
    
  2. For DialogBox use composition instead of inheritance since now your SearchDialogBox extends BaseEventHandler

  3. Annotate your SearchDialogBox with @EventHandler (and @Singleton if needed)
  4. In your YOUR_EVENT_BUS create at least one method that should be processed in SearchDialogBox

    • for example in YOUR_EVENT_BUS:

      @Event(handlers = {SearchDialogBox.class}) void helloWorld();

      and in SearchDialogBox

      `public void onHelloWorld(){...}`
      
于 2013-10-18T17:14:59.823 回答