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
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).