Skip to content

Need a way to dispatch a custom event to the DOM #3664

Closed

Description

@jmesserly

It doesn't appear possible to dispatch a custom event in the DOM. However, this works fine in JS. See:

http://api.jquery.com/category/events/event-object/
http://api.jquery.com/trigger/

FWIW, Closure has goog.events, but it looks like they don't try to use the DOM for dispatch (from a brief look at the code)

Activity

  1. DartBot commented on Jun 27, 2012

    @DartBot

    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?

  2. jmesserly commented on Jun 27, 2012

    @jmesserly
    Author

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

  3. DartBot commented on Jun 28, 2012

    @DartBot

    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?

  4. jmesserly commented on Jun 28, 2012

    @jmesserly
    Author

    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.

  5. DartBot commented on Jul 4, 2012

    @DartBot

    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?

  6. jmesserly commented on Jul 9, 2012

    @jmesserly
    Author

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

  7. jmesserly commented on Jul 9, 2012

    @jmesserly
    Author

    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.

  8. jmesserly commented on Aug 7, 2012

    @jmesserly
    Author
  9. iposva-google commented on Aug 13, 2012

    @iposva-google
    Contributor

    Removed Area-DOM label.
    Added Area-HTML label.

  10. vsmenon commented on Aug 15, 2012

    @vsmenon
    Member

    Is extending Event part of WebComponents?


    Added this to the M1 milestone.

  11. DartBot commented on Aug 15, 2012

    @DartBot

    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.

  12. jmesserly commented on Aug 15, 2012

    @jmesserly
    Author

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

  13. vsmenon commented on Sep 26, 2012

    @vsmenon
    Member
  14. efortuna commented on Sep 28, 2012

    @efortuna
    Contributor

    Set owner to @efortuna.

  15. efortuna commented on Oct 2, 2012

    @efortuna
    Contributor

    Constructor added to CustomEvent in r13127.


    Added Fixed label.

  16. added this to the M1 milestone on Oct 2, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

web-librariesIssues impacting dart:html, etc., libraries

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions