GWT 自定义事件处理程序 [英] GWT Custom Event Handler

查看:27
本文介绍了GWT 自定义事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个创建自定义事件和处理程序集的例子.假设您有一个 Person 对象,您希望小部件知道它是否已更新.

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.

您创建了一个 HandlerManager,现在您必须创建一个事件和一个处理程序.您将如何定义这些类以便您可以订阅和触发事件?

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?

大多数事件都是基于 DOM 的,而我想创建一些自定义事件和处理程序,我可以在任何基于浏览器的事件之外触发这些事件和处理程序.

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.

推荐答案

这是一个创建自定义事件的非常全面的示例,逐字摘自 GwtEventSystem Wiki(当事件系统仍在 GWT 的孵化器中时).

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);
}

添加唯一的事件类型

class HappyEvent extends AbstractEvent{
  public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}

  public GwtEvent.Key getKey(){
    return KEY; 
  }
  ...
}

连接处理程序的 fire 方法

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆