简体   繁体   English

GWT事件处理:针对侦听器类的选择性实例触发事件

[英]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. 我们有一个使用GWTGWTP框架提供MVP架构的应用程序。 We use GWT 's EventBus to broadcast events across presenters. 我们使用GWTEventBus在演示者之间广播事件。

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只能由实例接收AChild C ,而不是通过实例BChild 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM