在自定义对象上实现 EventTarget
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过多种方式执行此操作,一种方法可能是使用 typedef 来定义通用处理程序函数:
我为您制作了一个 DartBoard 代码段 这里 可以玩一下。
您可能想要尝试将 EventTarget 制作为基类,除非它会扰乱您的继承策略(在这种情况下,您可以使用工厂注入的事件总线可能更适合)。
You can do this in multiple ways, one method could be to use typedef's to define a generic handler function:
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).
我想你是的,但我更喜欢一种针对内部对象的更健壮的事件模型,而不是用于在 Dart 中包装 DOM 事件的模型。我在 .net 事件模型之后使用的模型更多,并且允许传递源对象和携带与事件相关的数据的 EventArgs 类(或子类)。
https://github .com/LUCA-Studios-LLC/LUCA-UI-Framework-for-Dart/blob/master/core/FrameworkEvent.dart
如下所示:
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: