如何使用 GWT EventBus
我想知道如何使用EventBus
或者是否有一些更好的解决方案来通过项目发送Event
。
Widget1
有一个Button
。 Widget2
有一个 Label
,当我按下按钮时它应该改变。这些小部件位于 DockLayout
中:
RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();
DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);
dock.addWest(new Widget1(), 10);
dock.add(new Widget2());
rootLayoutPanel.add(dock);
我在 Widget1
中声明了一个 handleClickAlert
:
@UiHandler("button")
void handleClickAlert(ClickEvent e) {
//fireEvent(e);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将项目划分为逻辑部分(例如 MVP)时,不同的部分有时需要进行通信。通常,此通信是通过发送状态更改来完成的,例如:
在这些情况下,使用事件总线是非常合乎逻辑的。
要使用它,您需要为每个应用实例化一个
EventBus
,然后由所有其他类使用。要实现此目的,请使用静态字段、工厂或依赖项注入(GWT 中为 GIN)。使用您自己的事件类型的示例:
通常您还可以创建自己的事件类型和处理程序:
以及处理程序:
然后您可以像这样使用它:
并触发事件:
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: