在自定义对象上实现 EventTarget

发布于 2024-12-22 15:39:24 字数 753 浏览 3 评论 0原文

使用 Closure Library,您可以让任何对象能够调度扩展 goog 的事件.events.EventTarget。目前可以使用 Dart 库实现这一点吗?

我想它会是这样的:

#import('dart:html');

class Foo implements EventTarget {
  Events get on() {
    // ???
  }
}

main() {
  Foo foo = new Foo();

  // Subscribe to the event.
  foo.on['bar'].add((Event event) => print('bar!'));

  // Dispatch the event.
  foo.on['bar'].dispatch(new Event('bar'));
}

我走在正确的道路上吗?

编辑 感谢 Lars Tackmann,工作草案如下:http://try。 dartlang.org/s/f6wk

Using Closure Library, you can give any object the ability to dispatch events extending goog.events.EventTarget. Is this currently possible using Dart libraries?

I imagine it would look like this:

#import('dart:html');

class Foo implements EventTarget {
  Events get on() {
    // ???
  }
}

main() {
  Foo foo = new Foo();

  // Subscribe to the event.
  foo.on['bar'].add((Event event) => print('bar!'));

  // Dispatch the event.
  foo.on['bar'].dispatch(new Event('bar'));
}

Am I on the right track?

Edit Thanks to Lars Tackmann, a working draft is here: http://try.dartlang.org/s/f6wk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-29 15:39:24

您可以通过多种方式执行此操作,一种方法可能是使用 typedef 来定义通用处理程序函数:

class Event {
   final String type;
   Event(this.type);
}

typedef EventHandler(Event event);

interface EventTarget {
  Map<String,EventHandler> get on();
  void dispatch(Event event);
}

class Foo implements EventTarget {
  Map<String, EventHandler> _handlers;
  Foo() {
    _handlers = new Map();
  } 

  Map<String, EventHandler> get on() {
    return _handlers;
  }

  dispatch(Event event) {
     EventHandler handler = _handlers[event.type];
     handler(event);
  }
}

main() {
  Foo foo = new Foo();

  foo.on['bar'] = (Event event) => print('handling event ${event.type}');

  foo.dispatch(new Event('bar'));
}

我为您制作了一个 DartBoard 代码段 这里 可以玩一下。

您可能想要尝试将 EventTarget 制作为基类,除非它会扰乱您的继承策略(在这种情况下,您可以使用工厂注入的事件总线可能更适合)。

You can do this in multiple ways, one method could be to use typedef's to define a generic handler function:

class Event {
   final String type;
   Event(this.type);
}

typedef EventHandler(Event event);

interface EventTarget {
  Map<String,EventHandler> get on();
  void dispatch(Event event);
}

class Foo implements EventTarget {
  Map<String, EventHandler> _handlers;
  Foo() {
    _handlers = new Map();
  } 

  Map<String, EventHandler> get on() {
    return _handlers;
  }

  dispatch(Event event) {
     EventHandler handler = _handlers[event.type];
     handler(event);
  }
}

main() {
  Foo foo = new Foo();

  foo.on['bar'] = (Event event) => print('handling event ${event.type}');

  foo.dispatch(new Event('bar'));
}

I made a DartBoard snippet for you here to play around with.

You might want to experiment with making EventTarget into a base class, unless of cause it messes up your inheritance strategy (in which case a event bus you can inject with a factory might be a better fit).

梦旅人picnic 2024-12-29 15:39:24

我想你是的,但我更喜欢一种针对内部对象的更健壮的事件模型,而不是用于在 Dart 中包装 DOM 事件的模型。我在 .net 事件模型之后使用的模型更多,并且允许传递源对象和携带与事件相关的数据的 EventArgs 类(或子类)。

https://github .com/LUCA-Studios-LLC/LUCA-UI-Framework-for-Dart/blob/master/core/FrameworkEvent.dart

如下所示:

FrameworkEvent<EventArgs> myEvent = new FrameworkEvent<EventArgs>();

//here subscribing using custom operator override "+"    
var handler = myEvent + (Dynamic source, EventArgs args) {
 //do stuff when the event fires here
};

//fire the event
myEvent.invoke(this, new EventArgs());

//unsubscribe using operator override "-"
myEvent - handler; //unsubscribe from the event here

I think you are, but I favor a more robust event model for internal objects than the one used to wrap DOM events in Dart. The one I use models more after the .net event model, and allows passing of both the source object, and an EventArgs class (or sub-class) carrying data related to the event.

https://github.com/LUCA-Studios-LLC/LUCA-UI-Framework-for-Dart/blob/master/core/FrameworkEvent.dart

looks like:

FrameworkEvent<EventArgs> myEvent = new FrameworkEvent<EventArgs>();

//here subscribing using custom operator override "+"    
var handler = myEvent + (Dynamic source, EventArgs args) {
 //do stuff when the event fires here
};

//fire the event
myEvent.invoke(this, new EventArgs());

//unsubscribe using operator override "-"
myEvent - handler; //unsubscribe from the event here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文