You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need a way to dispatch a custom event to the DOM #3664
may you, please, provide more details? There is CustomEvent interface in DOM and I believe you should be able to use. May you give a snippet that fails?
Furthermore, it is an interface not a class, so there's no easy way to subclass and add a field, without reimplementing all of Event (a big interface).
But extending DOM interfaces with your own implementation is completely different issue. I don't think Dart will support a case when you implement CustomEvent interface and expect it to behave as a regular custom event. Ditto for nodes.
Parkour folks are looking for something like that, but that's very challenging technically.
To consider: WebKit operates on native objects. One can extends CustomEvent implementation class, and even create an instance of it. But now consider what goes on when events is dispatched: we call into bindings, extract C++ counterpart out of DOM wrapper, pass into WebKit proper, and when this C++ counterpart comes back to us, we somehow should be able to understand that it has a separate wrapper for it.
I don't want to say it's technically impossible to implement something like that, but there would be tons of corner cases to sort out.
Just curious, why you need your own custom CustomEvent?
Thanks for the link to the test. That is helpful.
Once CustomEvent has a constructor, it will be much more discoverable :)
I guess the best we can do for now is dispatch a CustomEvent with "detail" set to a Dart payload. That is pretty useful. FWIW, if we could make it CustomEvent<T> with T detail that would be even better.
Why I need my own custom event: when you're building UI controls, you generally have new events you want to expose with your own payload data. If you can't reuse the DOM events, you end up inventing a parallel but slightly different dispatch system on the side for your widgets.
I can see why extending Element is hard to make work. But does extending Event pose the same challenges? It never appears in the tree, any instances of subclasses would always be created in Dart code, and after you call "dispatch" those objects come right back into Dart code at predictable points (the callbacks of addEventListener) and predictable times (right after dispatch). Part of the challenge here is I'm comparing us to JS. In JS it's trivial to attach as much data as you want to an Event. Whereas in Dart, subclassing is the only way to add new fields to a class. =(
Then again, if we could make CustomEvent<T> work, and use the T detail field for storing typed event data, that should be good enough in practice.
The challenge is I need a way to have a strongly typed event:
class ScrollEvent extends Event {
final num scrollLeft, scrollTop;
ScrollEvent(String type, this.scrollLeft, this.scrollTop)
: super(type, bubbles: true, cancelable: false);
}
Declared with something like:
interface ScrollEvents extends ElementEvents {
EventListenerList<ScrollEvent> get scrollStart();
EventListenerList<ScrollEvent> get scrollEnd();
EventListenerList<ScrollEvent> get scrollMove();
}
They don't add it to the webcomponents spec because it already works in JS :). See my original links up top.
The difference between Event and Element is for Element you need a way to construct it at HTML document parse time (or at least fixup the node after parse time) . Whereas custom events are fired from code, so it's easy to add data such as fields or method from your JS code. We need the same capability from Dart--ideally via subclassing Event (and friends).
Activity
DartBot commentedon Jun 27, 2012
This comment was originally written by [email protected]
John,
may you, please, provide more details? There is CustomEvent interface in DOM and I believe you should be able to use. May you give a snippet that fails?
jmesserly commentedon Jun 27, 2012
There isn't a constructor for CustomEvent, how am I supposed to create one?
http://api.dartlang.org/html/CustomEvent.html
Furthermore, it is an interface not a class, so there's no easy way to subclass and add a field, without reimplementing all of Event (a big interface).
DartBot commentedon Jun 28, 2012
This comment was originally written by [email protected]
Yep, a constructor is still missing (Stephen is working on it), but you should be able to do that anyway: http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/html/event_customevent_test.dart#31
But extending DOM interfaces with your own implementation is completely different issue. I don't think Dart will support a case when you implement CustomEvent interface and expect it to behave as a regular custom event. Ditto for nodes.
Parkour folks are looking for something like that, but that's very challenging technically.
To consider: WebKit operates on native objects. One can extends CustomEvent implementation class, and even create an instance of it. But now consider what goes on when events is dispatched: we call into bindings, extract C++ counterpart out of DOM wrapper, pass into WebKit proper, and when this C++ counterpart comes back to us, we somehow should be able to understand that it has a separate wrapper for it.
I don't want to say it's technically impossible to implement something like that, but there would be tons of corner cases to sort out.
Just curious, why you need your own custom CustomEvent?
jmesserly commentedon Jun 28, 2012
Thanks for the link to the test. That is helpful.
Once CustomEvent has a constructor, it will be much more discoverable :)
I guess the best we can do for now is dispatch a CustomEvent with "detail" set to a Dart payload. That is pretty useful. FWIW, if we could make it CustomEvent<T> with T detail that would be even better.
Why I need my own custom event: when you're building UI controls, you generally have new events you want to expose with your own payload data. If you can't reuse the DOM events, you end up inventing a parallel but slightly different dispatch system on the side for your widgets.
I can see why extending Element is hard to make work. But does extending Event pose the same challenges? It never appears in the tree, any instances of subclasses would always be created in Dart code, and after you call "dispatch" those objects come right back into Dart code at predictable points (the callbacks of addEventListener) and predictable times (right after dispatch). Part of the challenge here is I'm comparing us to JS. In JS it's trivial to attach as much data as you want to an Event. Whereas in Dart, subclassing is the only way to add new fields to a class. =(
Then again, if we could make CustomEvent<T> work, and use the T detail field for storing typed event data, that should be good enough in practice.
DartBot commentedon Jul 4, 2012
This comment was originally written by [email protected]
John, there is actually Expando interface in corelib: http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/corelib/src/expando.dart
I don't think it's been implemented for VM yet, but provided it will be implemented eventually, will it solve your problem?
jmesserly commentedon Jul 9, 2012
The challenge is I need a way to have a strongly typed event:
class ScrollEvent extends Event {
final num scrollLeft, scrollTop;
ScrollEvent(String type, this.scrollLeft, this.scrollTop)
: super(type, bubbles: true, cancelable: false);
}
Declared with something like:
interface ScrollEvents extends ElementEvents {
EventListenerList<ScrollEvent> get scrollStart();
EventListenerList<ScrollEvent> get scrollEnd();
EventListenerList<ScrollEvent> get scrollMove();
}
interface EventListenerList<T extends Event> {
EventListenerList<T> add(EventListener<T> handler, [bool useCapture]);
EventListenerList<T> remove(EventListener<T> handler, [bool useCapture]);
bool dispatch(T evt);
}
class MyScroller extends View {
ScrollEvents get on() => ...;
}
typedef void EventListener<T extends Event>(T event);
The reason for the types is so a user (and tools like the Dart Editor) knows what kind of payload data will be passed.
(OTOH, if Expando was supported in the VM, I wonder if it could be used to make subclassing Event work. Haven't thought much about that, though.)
jmesserly commentedon Jul 9, 2012
BTW, for now we've been working around this by routing events ourselves in Dart code. It works okay assuming a global event listener, but it's clunky.
jmesserly commentedon Aug 7, 2012
Update: this is going to become more important if we want to get web components (http://dvcs.w3.org/hg/webcomponents/raw-file/tip/explainer/index.html) working. That also needs to extend Elements too.
iposva-google commentedon Aug 13, 2012
Removed Area-DOM label.
Added Area-HTML label.
vsmenon commentedon Aug 15, 2012
Is extending Event part of WebComponents?
Added this to the M1 milestone.
DartBot commentedon Aug 15, 2012
This comment was originally written by [email protected]
Not as far as I know in the javascript spec -- I'm happy to discuss adding it to the dart implementation.
jmesserly commentedon Aug 15, 2012
@vsm: yes.
They don't add it to the webcomponents spec because it already works in JS :). See my original links up top.
The difference between Event and Element is for Element you need a way to construct it at HTML document parse time (or at least fixup the node after parse time) . Whereas custom events are fired from code, so it's easy to add data such as fields or method from your JS code. We need the same capability from Dart--ideally via subclassing Event (and friends).
vsmenon commentedon Sep 26, 2012
We'll add a CustomEvent constructor to close off this bug.
I've created a separate bug for subtyping:
http://code.google.com/p/dart/issues/detail?id=5478
efortuna commentedon Sep 28, 2012
Set owner to @efortuna.
efortuna commentedon Oct 2, 2012
Constructor added to CustomEvent in r13127.
Added Fixed label.
[deps] rev native, test, tools, web