1

Upon reading this question I wondered if it is really needed to extend CustomEvent if you want to have custom DOM events in your application. Would it be possible to just extend Event or does CustomEvent serve a special purpose for technical reasons?

An example I was thinking of: a login component with 2 properties "username" and "password". I can imagine a LoginEvent with those two properties. I could either:

  • have the two properties as properties of the event class, in which case I don't need/use the detail of CustomEvent and hence may not need to extend CustomEvent.
  • pass them in as an array to the detail property of CustomEvent.
  • create a LoginParams object with the two properties and pass this in to the detail property of CustomEvent.
Community
  • 1
  • 1
Christophe Herreman
  • 15,895
  • 9
  • 58
  • 86

1 Answers1

0

CustomEvent.detail is meant exactly for this, though the types must be passable to JS so there are some restrictions.

It'd be nice if you could extend CustomEvent, but that had similar issues as extending Element.

Justin Fagnani
  • 10,483
  • 2
  • 27
  • 37
  • I understand CustomEvent can be used for this scenario. I was wondering if it is really needed to use it. What if I don't want to use the generic detail property of CustomEvent. Can I create my own custom event by extending Event instead? – Christophe Herreman Sep 02 '13 at 20:33
  • If you want to take advantage of how the DOM handles events, like bubbling, then you need to use some subclass of dart.html.Event. Otherwise you can just create a Stream of any type. – Justin Fagnani Sep 02 '13 at 21:03
  • Also, I'd consider events to be a potential point of JS interop, and using DOM helps ensure compatibility. – Justin Fagnani Sep 02 '13 at 21:05