Skip to main content

Bug Tracker

Side navigation

#4537 closed bug (fixed)

Opened April 15, 2009 06:05AM UTC

Closed February 10, 2011 09:22PM UTC

Last modified March 09, 2012 10:35AM UTC

clone(true) with namespaced events loses the namespace on the original and the clone

Reported by: ktrott Owned by: jitter
Priority: high Milestone: 1.5.1
Component: manipulation Version: 1.5
Keywords: event namespace clone Cc:
Blocked by: Blocking:
Description

The clone(true) method does not properly preserve the namespaced events. The events are cloned but the namespace aspect of the event is lost not only on the clone but also on the original. I believe this is because the original element's event handler is reused for the clone and the handler.type is overwritten even though it already has a type on it. I believe in the clone method, before calling add event, the full type needs to be reconstructed from the type and the handler.type OR the event.add method should not overwrite handler.type but append to it?

Here is example code that should highlight the issue:

jQuery('<div id="test1">Test test test</div>').appendTo(jQuery(document.body));

jQuery("#test1").bind("click.test", function(){alert("test");});

works

jQuery("#test1").trigger("click.test");

jQuery("#test1").clone(true).attr("id", "test2").appendTo(jQuery(document.body));

fails

jQuery("#test1").trigger("click.foo");

works

jQuery("#test1").trigger("click");

fails

jQuery("#test2").trigger("click.foo");

// works

jQuery("#test2").trigger("click");

Attachments (0)
Change History (11)

Changed April 15, 2009 06:17AM UTC by ktrott comment:1

I messed up the example. Please ignore the first example and use this:

jQuery('<div id="test1">Test test test</div>').appendTo(jQuery(document.body));

jQuery("#test1").bind("click.test", function(){alert("test");});

works

jQuery("#test1").trigger("click.test");

jQuery("#test1").clone(true).attr("id", "test2").appendTo(jQuery(document.body));

fails

jQuery("#test1").trigger("click.test");

works

jQuery("#test1").trigger("click");

fails

jQuery("#test2").trigger("click.test");

// works

jQuery("#test2").trigger("click");

Changed July 23, 2009 02:08PM UTC by carmel_dev comment:2

I am also seeing this issue arise when cloning elements with plugins applied to them. The plugin uses name-spaced events and after the clone we are unable to remove the plugin using the plugin's remove event.

Any update for this issue?

Changed August 13, 2010 08:54PM UTC by mlemay comment:3

This bug still exists in jquery-1.4.2. I fixed it to make it work in a local version I'm saving. Here's a diff of the changes from http://code.jquery.com/jquery-1.4.2.js:

4345c4345,4350

< jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );


var event_type = type; if((events[type][handler].namespace != null) && (events[type][handler].namespace.length > 0)) { event_type += '.' + events[type][handler].namespace; } jQuery.event.add( this, event_type, events[ type ][ handler ], events[ type ][ handler ].data );

Changed October 25, 2010 06:46AM UTC by SlexAxton comment:4

keywords: → event namespace clone
milestone: 1.41.5
owner: → ktrott
priority: majorlow
status: newpending

Can you provide a testcase on jsFiddle so we can try and get this patch in? Thanks!

Changed October 28, 2010 07:13AM UTC by anonymous comment:5

Changed November 11, 2010 11:09PM UTC by trac-o-bot comment:6

status: pendingclosed

Automatically closed due to 14 days of inactivity.

Changed January 26, 2011 12:25PM UTC by Martijn Faassen comment:7

Was this patch ever applied? It seems to have been closed automatically?

Changed January 27, 2011 12:48PM UTC by jitter comment:8

component: coremanipulation
milestone: 1.51.5.1
priority: lowhigh
status: closedreopened
version: 1.3.21.5rc1

Doesn't look like this was fixed. As the original reporter didn't provide a test case (or did without logging in) this one slipped through the cracks because the ticket system didn't notice that was a test case attached.

Changed January 27, 2011 12:48PM UTC by jitter comment:9

owner: ktrottjitter
status: reopenedassigned

Changed February 10, 2011 09:22PM UTC by Anton M comment:10

resolution: → fixed
status: assignedclosed

Make sure .clone(true) correctly clones namespaced events. Fixes #4537.

Changeset: 78fc79fad47ce2991c0a7148b65acd7221223eb9

Changed February 10, 2011 09:25PM UTC by jitter comment:11

version: 1.5rc11.5