W3C home > Mailing lists > Public > public-webapps@w3.org > January to March 2012

[DOM4] EventTarget as first class citizen

From: Marcos Caceres <w3c@marcosc.com>
Date: Thu, 23 Feb 2012 11:21:07 +0000
To: public-webapps <public-webapps@w3.org>
Message-ID: <33467E275BEE4E1CAB6B67840D0E20BF@marcosc.com>
Would it be possible for DOM4 to define a way for user objects to be able to extend EventTarget (as in Object.create(EventTarget))?

The use case: give the ability to create objects that can dispatch events using native means… instead of hacking around it like so:  

var obj = Object.create(null);
var dispatcher = document.createElement("x-EventDispatcher");  

//implement EventTarget interface on obj  
Object.defineProperty(obj, "addEventListener", {
value: function(type, callback, capture){
dispatcher.addEventListener(type, callback, capture);  

Object.defineProperty(obj, "removeEventListener", {
value: function(type, callback, capture){
dispatcher.removeEventListener(type, callback, capture);  

Object.defineProperty(obj, "dispatchEvent", {
value: function(e){

var e = document.createEvent('CustomEvent');
e.initEvent("myEvent", false, false, null);  

Also, AFAIK, all JS frameworks have implemented custom ways of handling events and how they are dispatched, so clearly its a desired part of the platform. For example:

Developers have also built their own:

Other solutions fire at the document, which means registering listeners on an object that is not the one that needs to receive the event:

So, together with CustomEvents provided by the platform, it would be nice to have a custom EventTarget to fire those things at :)   

Kind regards,
Marcos Caceres
Received on Thursday, 23 February 2012 11:21:41 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:13:31 UTC