I wonder how to use the EventBus
or whether there are some better solutions to send an Event
through the project.
Widget1
has a Button
. Widget2
has a Label
, that should change when I press the button. These widgets are in a DockLayout
:
RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();
DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);
dock.addWest(new Widget1(), 10);
dock.add(new Widget2());
rootLayoutPanel.add(dock);
I have declared an handleClickAlert
in Widget1
:
@UiHandler("button")
void handleClickAlert(ClickEvent e) {
//fireEvent(e);
}
When you divide the project into logical parts (for example with MVP), then the different parts sometimes need to communicate. Typical this communication is done by sending status changes, e.g.:
Using the event bus is quite logical in those cases.
To use it you instantiate one
EventBus
per app which is then used by all other classes. To achieve this use a static field, factory or dependency injection (GIN in case of GWT).Example with your own event types:
Normally you'd also create your own event types and handlers:
and the handler:
Then you use it like this:
and fire the event: