Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18099

Re: Clone node with events

Message-ID <4079901.yXqkfi7LMG@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-01-13 15:07 +0100
Subject Re: Clone node with events
Newsgroups comp.lang.javascript
References <kcm8um$8pi$1@speranza.aioe.org> <3cfb8a75-8706-42d6-9cc5-fbd293ee29f9@10g2000yqo.googlegroups.com> <kcsm8o$8of$1@speranza.aioe.org>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Cezary Tomczyk wrote:

> […]
> consider this simple example:
> 
> http://jsfiddle.net/JpLx2/
> 
> All I need here is to just copy attached events from old_node to
> new_node, but seems that is not so easy to do. This could be possible
> done by, for example, adding some wrapper to event methods and then use
> it.

First of all, you are _not_ copying events.  You could be copying event 
*listeners*, but all you would copy in ECMAScript would be *references* to 
Function instances.

So there is no problem here.  You have a reference to a Function instance, 
and the “click” event bubbles in all know DOM implementations.  So, as 
already suggested, you should use event delegation, in particular use event 
bubbling: Add to a common ancestor element a listener for that event that 
does actions only for specific event targets.  When a cloned target receives 
that event, it has no listener for it; since the event bubbles, it will be 
handled by the ancestor element which has a listener for it.

BTW, you should not be using attachEvent() as an alternative to 
addEventListener():

<http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html>

> On the other hand I found
> http://www.w3.org/TR/2001/WD-DOM-Level-3
> Events-20010823/events.html#Events-EventListenerList,
> but seems that it is not implemented, for example, in Firefox 17.0.1 and
> Opera 12.12. Probably in other browsers also is not implemented :-(

Neither is it implemented in WebKit WebCore as of Chromium 22.  You are 
referring to an *old* Working Draft.  The *current* Working Draft for W3C 
DOM Level 3 Events is available at

<http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120906/>.

You will notice that the EventTarget interface no longer has an 
“eventListeners” attribute of type “EventListenerList”, and that the 
“EventListenerList” interface has been removed:

<http://www.w3.org/TR/2012/WD-DOM-Level-3-Events-20120906/#interface-
EventTarget>

And it would seem that no replacement for that attribute or interface has 
been suggested yet.

However, with proprietary event handler properties (called “traditional 
event registration” on QuirksMode.org) now perhaps going to be a Web 
standard with the HTML5 API, for elements and events for which only one 
event listener has been added, you could use those properties to retrieve 
the primary event listener for an event on an element:
 
<http://www.w3.org/html/wg/drafts/html/master/index.html#ix-event-handlers>
<http://www.w3.org/html/wg/drafts/html/master/webappapis.html#event-
handlers>

While that has worked even before HTML5 (jsx.dom.*EventListener() are based 
on it), I recommend you use event bubbling here.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-10 12:33 +0100
  Re: Clone node with events Scott Sauyet <scott.sauyet@gmail.com> - 2013-01-10 07:30 -0800
    Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-12 22:57 +0100
      Re: Clone node with events Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-13 15:07 +0100
        Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-16 20:50 +0100
  Re: Clone node with events Stefan Weiss <krewecherl@gmail.com> - 2013-01-10 17:15 +0100
    Re: Clone node with events Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-12 23:00 +0100
  Re: Clone node with events RobG <rgqld@iinet.net.au> - 2013-01-13 22:07 -0800

csiph-web