How to use EventBus for non-Presenter class in GWTP?

邮差的信 提交于 2019-12-10 11:45:17

问题


I am using GWTP platform & eClipse to build webapp. In Eclipse, when creating a Presenter, it will create 3 files (ex: 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;
    }
}

To use eventBus, we just simply use eclipse to create EventBus file, ex MyEvent.java, then we call eventBus in SearchPresenter by using this code:

MyEvent mEvent=new MyEvent();
SearchPresenter.this.eventBus.fireEvent(mEvent);

now suppose I got a non-presenter class public class SearchDialogBox extends DialogBox, then my question is how can i use MyEvent in SearchDialogBox? How to getEventBus() in SearchDialogBox?


回答1:


I' dont use GWTP, but I guess the following is ok.

@Inject private EventBus eventBus

should work (if you don't use it in SearchDialogBox constructor right away).

Otherwise try to find out which class in GWTP extends com.google.gwt.inject.client.Ginjector. Assuming it is called "MyInjector" just write :

private EventBus eventBus = MyInjector.INSTANCE.getEventBus();



回答2:


Have a look at https://github.com/ArcBees/GWTP/wiki/Events.

You basically implement HasHandlers interface inject yourself the EventBus.




回答3:


  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(){...}`
      


来源:https://stackoverflow.com/questions/19432624/how-to-use-eventbus-for-non-presenter-class-in-gwtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!