Extracted the logic for copying events from one jQuery set to another, makes it easier to work with disconnected DOM nodes.

This commit is contained in:
jeresig 2009-12-02 17:15:09 -05:00
parent 391f83b2a2
commit 62436f4b29

View file

@ -168,21 +168,8 @@ jQuery.fn.extend({
// Copy the events from the original to the clone // Copy the events from the original to the clone
if ( events === true ) { if ( events === true ) {
var orig = this.find("*").andSelf(), i = 0; cloneCopyEvent( this, ret );
cloneCopyEvent( this.find("*"), ret.find("*") );
ret.find("*").andSelf().each(function(){
if ( this.nodeName !== orig[i].nodeName ) { return; }
var events = jQuery.data( orig[i], "events" );
for ( var type in events ) {
for ( var handler in events[ type ] ) {
jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
}
}
i++;
});
} }
// Return the cloned set // Return the cloned set
@ -284,6 +271,24 @@ jQuery.fn.extend({
} }
}); });
function cloneCopyEvent(orig, ret) {
var i = 0;
ret.each(function(){
if ( this.nodeName !== orig[i].nodeName ) {
return;
}
var events = jQuery.data( orig[i], "events" );
for ( var type in events ) {
for ( var handler in events[ type ] ) {
jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
}
}
});
}
function buildFragment(args, nodes, scripts){ function buildFragment(args, nodes, scripts){
var fragment, cacheable, cached, cacheresults, doc; var fragment, cacheable, cached, cacheresults, doc;