Bug Tracker

Modify

Ticket #4537 (closed bug: fixed)

Opened 5 years ago

Last modified 2 years ago

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:
Blocking: Blocked by:

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");

Change History

comment:1 in reply to: ↑ description Changed 5 years ago by ktrott

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");

comment:2 Changed 5 years ago by carmel_dev

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?

comment:3 Changed 4 years ago by mlemay

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 );

comment:4 Changed 3 years ago by SlexAxton

  • Keywords event namespace clone added
  • Owner set to ktrott
  • Priority changed from major to low
  • Status changed from new to pending
  • Milestone changed from 1.4 to 1.5

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

comment:6 Changed 3 years ago by trac-o-bot

  • Status changed from pending to closed

Automatically closed due to 14 days of inactivity.

comment:7 Changed 3 years ago by Martijn Faassen

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

comment:8 Changed 3 years ago by jitter

  • Priority changed from low to high
  • Status changed from closed to reopened
  • Version changed from 1.3.2 to 1.5rc1
  • Component changed from core to manipulation
  • Milestone changed from 1.5 to 1.5.1

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.

comment:9 Changed 3 years ago by jitter

  • Owner changed from ktrott to jitter
  • Status changed from reopened to assigned

comment:10 Changed 3 years ago by Anton M

  • Status changed from assigned to closed
  • Resolution set to fixed

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

Changeset: 78fc79fad47ce2991c0a7148b65acd7221223eb9

comment:11 Changed 3 years ago by jitter

  • Version changed from 1.5rc1 to 1.5

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.