简体   繁体   English

如果我在GWT中扩展Widget,如何使用祖先的EventBus?

[英]How to use ancestor's EventBus if I am extending Widget in GWT?

I am extending a ScrollPanel and wish to process my own custom events with this new widget. 我正在扩展ScrollPanel并希望使用这个新小部件处理我自己的自定义事件。

I made my own HasMyHandlers interface with two methods fireEvent(MyEvent event) and HandlerRegistration addMyHandler(MyHandler handler) . 我用两个方法fireEvent(MyEvent event)HandlerRegistration addMyHandler(MyHandler handler)了我自己的HasMyHandlers接口。

First I made a private member SimpleEventBus eventBus , but next thought that the ancestor class should already have it's own copy. 首先,我创建了一个私有成员SimpleEventBus eventBus ,但接下来认为祖先类应该已经拥有它自己的副本。

Is it possible to use ancestor's event bus, ie to register handlers within it and to fire them according to it? 是否可以使用祖先的事件总线,即在其内部注册处理程序并根据其触发它们?

Yes, it is possible. 对的,这是可能的。 The standard way to do this is in the implementation of HandlerRegistration addMyHandler(MyHandler handler) is : 执行此操作的标准方法是在HandlerRegistration addMyHandler(MyHandler handler)中:

public class MyWidget extends Widget implements HasMyHandlers {

  @Override
  public HandlerRegistration addMyHandler(MyHandler handler) { 
    return addHandler(handler, MyEvent.getType());
  }

  // Other methods
}

The Widget.addHandler(...) method provides the mechanism to wire custom event handlers to the Widget's HandlerManager. Widget.addHandler(...)方法提供了将自定义事件处理程序连接到Widget的HandlerManager的机制。

To fire your event to all registered handlers you use Widget.fireEvent(...) method. 要将事件Widget.fireEvent(...)到所有已注册的处理程序,请使用Widget.fireEvent(...)方法。 So to fire your event you can do: 因此,您可以执行以下操作:

fireEvent(new MyEvent(yourEventData));

It's worth noting that your event should also extend the GwtEvent class. 值得注意的是,您的事件还应该扩展GwtEvent类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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