Fw: [DOM4] EventTarget as first class citizen

Hi Scriptlib CG,  
I would like your thoughts, comments (, and hopefully support!) on a proposed change to DOM4… Basically, I want DOM4 to allow objects that implement EventTarget to be constructed so that they can be used freely outside DOM nodes. More details below, including use cases, evidence that people want it, and a response from Mozilla about possible syntax.  

In particular, I would appreciate thoughts about the API itself (i.e., how could this be made to work with constructed user objects, if possible… could we make use of "trees" or should it just be a graph-based event registration model? ).     

The discussion is happening here: http://lists.w3.org/Archives/Public/www-dom/2012JanMar/0102.html


Forwarded message:

> From: Jonas Sicking <jonas@sicking.cc>
> To: Marcos Caceres <w3c@marcosc.com>
> Cc: public-webapps <public-webapps@w3.org>
> Date: Thursday, 23 February 2012 15:18:30
> Subject: Re: [DOM4] EventTarget as first class citizen
>  
> On Thu, Feb 23, 2012 at 12:21 PM, Marcos Caceres <w3c@marcosc.com (mailto:w3c@marcosc.com)> wrote:
> > Hi,
> > 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){
> > dispatcher.dispatchEvent(e);
> > }
> > });
> >  
> >  
> >  
> > var e = document.createEvent('CustomEvent');
> > e.initEvent("myEvent", false, false, null);
> > dispatcher.dispatchEvent(e);
> >  
> >  
> >  
> > 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:
> >  
> > http://developer.yahoo.com/yui/docs/YAHOO.util.CustomEvent.html
> > http://api.jquery.com/category/events/event-object/
> > http://dojotoolkit.org/reference-guide/quickstart/events.html
> >  
> > Developers have also built their own:
> > http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/
> >  
> > Other solutions fire at the document, which means registering listeners on an object that is not the one that needs to receive the event:
> > http://tiffanybbrown.com/2011/10/12/dispatching-custom-dom-events/
> >  
> > So, together with CustomEvents provided by the platform, it would be nice to have a custom EventTarget to fire those things at :)
>  
> I think the way we should do this is to enable instantiating
> EventTarget objects using an EventTarget ctor. This won't give you a
> neato extension syntax, but I think we'll have to rely on ES.next for
> that. Before ecma-script has a better solution you can always
> monkeypatch the object.
>  
> So something like:
> a = new EventTarget();
> b = new EventTarget(a); // a is the parent in the target chain
>  
> should be doable. I don't know if we need a way to modify the parent
> chain after construction. It's somewhat complex to allow this while
> still preventing cycles from being created.
>  
> / Jonas  

Received on Friday, 2 March 2012 09:12:54 UTC