[英]GWT event handling: Fire events for selective instances of listener classes
Background: 背景:
We have an application using GWT
and the GWTP
framework providing the MVP
architecture. 我们有一个使用
GWT
和GWTP
框架提供MVP
架构的应用程序。 We use GWT
's EventBus
to broadcast events across presenters. 我们使用
GWT
的EventBus
在演示者之间广播事件。
Requirement: 需求:
We have an event called RefreshEvent
. 我们有一个称为
RefreshEvent
的事件。 The class/presenter firing the event is Parent(P)
and the presenter listening to this event is Child(C)
. 触发该事件的班级/演示者是
Parent(P)
,侦听此事件的演示者是Child(C)
。 We are running into a situation in the application where multiple instances of the C
(the listener) are active at the same time in the application. 我们在应用程序中遇到一种情况,其中
C
(侦听器)的多个实例在应用程序中同时处于活动状态。 Let's say instance A
and instance B
of presenter C
are active and both are listening for the RefreshEvent
. 假设演示者
C
实例A
和实例B
处于活动状态,并且都在监听RefreshEvent
。 But in a given context in the application I want to selectively fire the event. 但是在应用程序的给定上下文中,我想选择性地触发该事件。 That is a
RefreshEvent
fired by P
should be received only by instance A
of Child C
and not by instance B
of Child C
. 这是一个
RefreshEvent
被解雇P
只能由实例接收A
的Child C
,而不是通过实例B
的Child C
。
Does GWT
provide any mechanism to do this? GWT
是否提供任何机制来做到这一点? If not, how can I achieve this. 如果没有,我该如何实现。 Our application heavily uses events for inter presenter communication.
我们的应用程序大量使用事件进行演示者之间的通信。
Typically, EventBus is used to "broadcast" an event to each party that listens to it (in your example, all instances of your Child C). 通常,EventBus用于将事件“广播”给收听该事件的各方(在您的示例中,您的Child C的所有实例)。
If you want a single relationship, your parent can simply call the necessary child: 如果您想建立单一关系,您的父母可以简单地给必要的孩子打电话:
private Child a;
private Child b;
...
public void on onRefresh() {
/*
* If you need to call child a, call a.doSomething();
* If you need to call child b, call b.doSomething();
*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.