GWT自定义事件处理程序 [英] GWT Custom Event Handler
问题描述
假设你有一个Person对象,你想要你的小部件知道它是否被更新。
你创建一个HandlerManager,现在你必须创建一个事件一个处理者
您将如何定义这些类,以便您可以订阅和触发事件?
大多数事件都是基于DOM的,而我想要创建一些自定义事件和/或> h2_lin>解决方案
这是一个非常全面的创建自定义事件的例子,从 GwtEventSystem Wiki 逐字删除(当事件系统仍然在GWT的孵化器)。
这是一个事件,当用户开心时触发。
定义一个新的事件类。您可以在事件类中添加任意的元数据。为简单起见,我们不会包含任何内容。
public class HappyEvent extends GwtEvent {
...
}
为事件类定义一个新的处理程序和标记界面。
接口HappyHandler扩展EventHandler {
public void onHappiness(HappyEvent event);
}
接口HasHappyEvents {
public HandlerRegistration addHappyHandler(HappyHandler handler);
}
添加唯一的事件类型
class HappyEvent extends AbstractEvent {
public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}
public GwtEvent .Key getKey(){
return KEY;
}
...
}
fire方法
class HappyEvent extends GwtEvent {
static Key&HappyEvent,HappyHandler& KEY = new Key&HappyEvent,HappyHandler>(){
protected void fire(HappyHandler handler,HappyEvent event){
handler.onHappiness(event);
};
...
}
Can someone give me an example of creating a custom set of an Event and a Handler. Say you have a Person object that you want your widgets to know if it got updated.
You create a HandlerManager and now you have to create an Event and a Handler. How would you define those classes so that you can subscribe and fire events?
Most of the Events are DOM based, while I want to create some custom events and handlers that I can fire outside of any browser-based event.
Here's a pretty comprehensive example of creating a custom event, taken verbatim from the GwtEventSystem Wiki (when the event system was still in GWT's incubator).
This is an event that is triggered when the user becomes happy.
Define a new event class. You can add arbitrary metadata in the event class. For simplicity, we will not include any here though.
public class HappyEvent extends GwtEvent {
...
}
Define a new handler and marker interface for the event class.
interface HappyHandler extends EventHandler {
public void onHappiness(HappyEvent event);
}
interface HasHappyEvents {
public HandlerRegistration addHappyHandler(HappyHandler handler);
}
Add a unique event type
class HappyEvent extends AbstractEvent{
public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}
public GwtEvent.Key getKey(){
return KEY;
}
...
}
Wire up the handler's fire method
class HappyEvent extends GwtEvent {
static Key<HappyEvent,HappyHandler> KEY = new Key<HappyEvent,HappyHandler>(){
protected void fire(HappyHandler handler, HappyEvent event) {
handler.onHappiness(event);
};
...
}
这篇关于GWT自定义事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!