1

How to get com.google.web.bindery.event.shared.EventBus instance in com.google.gwt.user.client.ui.Composite extending class?;

I've readed: How to use EventBus for non-Presenter class in GWTP?, but I am looking for answer like:

BEST APPROACHES TO GWT DEVELOPMENT

I have widget that I would like to fire an event IndicatorEvent. But in this widget I don't have a handler for this event.

The handler is in other class. So to fire it I will use:

fireEvent(new IndicatorEvent("Custom Widget Message"));

Method fireEvent is avalible in com.google.web.bindery.event.shared;EventBus

public abstract void fireEvent(Event<?> event);

So I need an instance of EventBus in view class that extends Composite. This view doesn't have MVP stuff (presenters, uihandlers, etc.) it is simple one class element/widget.

What approach should I pick up?

  • Should I convert this to Presenter/View pair? (In gwtp presenters is EventBus reference from PresenterWidget class) And then using ui handlers, delegate execution from view to presenter?
  • Should I inject instance of EventBus into widget class?

Please help.

masterdany88
  • 5,041
  • 11
  • 58
  • 132

2 Answers2

2

Any Widget can fire an event without using the EventBus.

If you know the recipients of this event, you can simply attach handlers directly to the recipients. To do that, you define this method in your composite widget:

public HandlerRegistration addIndicatorEventHandler(IndicatorEventHandler handler) {
    return addHandler(handler, IndicatorEvent.TYPE);
}

and then in your other widget (recipient):

myCustomWidget.addIndicatorEventHandler(new IndicatorEventHandler() {

    @Override
    public void onIndicator(IndicatorEvent event) {
        // do something
    }

});

You will need to use EventBus if (a) there is no direct/easy link between the two widgets (firing event and event recipient), (b) there are many recipients, and/or (c) you do not want to create a dependency between parts of your code which contain event firing widget and event recipients.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
0

We can assign only single component per view. So approach first doesn't make any sense as we creates lots of component/widgets for all small sections from particular view.

Second approach is comparatively more likable if we are already using some dependency injection framework like gin or dagger2.

I can add one more approach is to have simple setter method in component for passing eventBus instance from the parent class like presenter or controller where we are using/creating component object.

My conclusion will be it absolutely depends upon purpose and implementation of component. If we have interface-implementation or presenter-implementation kind of component stating some common behavior second approach is best suitable way. On the other hand if we have simple plane component serving just as small part of view, I will recommend third approach.

Ajhar Shaikh
  • 1,106
  • 2
  • 14
  • 33