Commit d12e8a34 authored by John Resig's avatar John Resig

Made it so that you can bind a single function to multiple .live() selectors....

Made it so that you can bind a single function to multiple .live() selectors. Additionally, simplified the proxy code to provide a default proxy function. 
Fixes #3787.
parent eced38a3
...@@ -26,10 +26,7 @@ jQuery.event = { ...@@ -26,10 +26,7 @@ jQuery.event = {
var fn = handler; var fn = handler;
// Create unique handler function, wrapped around original handler // Create unique handler function, wrapped around original handler
handler = this.proxy( fn, function() { handler = this.proxy( fn );
// Pass arguments and context to original handler
return fn.apply(this, arguments);
});
// Store data in unique handler // Store data in unique handler
handler.data = data; handler.data = data;
...@@ -334,6 +331,7 @@ jQuery.event = { ...@@ -334,6 +331,7 @@ jQuery.event = {
}, },
proxy: function( fn, proxy ){ proxy: function( fn, proxy ){
proxy = proxy || function(){ return fn.apply(this, arguments); };
// Set the guid of unique handler to the same of original handler, so it can be removed // Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++; proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
// So proxy can be declared as an argument // So proxy can be declared as an argument
...@@ -362,7 +360,7 @@ jQuery.event = { ...@@ -362,7 +360,7 @@ jQuery.event = {
remove++; remove++;
}); });
if ( remove <= 1 ) if ( remove < 1 )
jQuery.event.remove( this, namespaces[0], liveHandler ); jQuery.event.remove( this, namespaces[0], liveHandler );
} }
} }
...@@ -546,12 +544,16 @@ jQuery.fn.extend({ ...@@ -546,12 +544,16 @@ jQuery.fn.extend({
}, },
live: function( type, fn ){ live: function( type, fn ){
jQuery(document).bind( liveConvert(type, this.selector), this.selector, fn ); var proxy = jQuery.event.proxy( fn );
proxy.guid += this.selector;
jQuery(document).bind( liveConvert(type, this.selector), this.selector, proxy );
return this; return this;
}, },
die: function( type, fn ){ die: function( type, fn ){
jQuery(document).unbind( liveConvert(type, this.selector), fn ); jQuery(document).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector } : null );
return this; return this;
} }
}); });
......
...@@ -474,7 +474,7 @@ test("toggle(Function, Function, ...)", function() { ...@@ -474,7 +474,7 @@ test("toggle(Function, Function, ...)", function() {
}); });
test(".live()/.die()", function() { test(".live()/.die()", function() {
expect(30); expect(34);
var submit = 0, div = 0, livea = 0, liveb = 0; var submit = 0, div = 0, livea = 0, liveb = 0;
...@@ -550,6 +550,31 @@ test(".live()/.die()", function() { ...@@ -550,6 +550,31 @@ test(".live()/.die()", function() {
jQuery("#anchor2").trigger("click"); jQuery("#anchor2").trigger("click");
equals( window.location.hash, hash, "e.preventDefault() worked" ); equals( window.location.hash, hash, "e.preventDefault() worked" );
jQuery("#anchor2").die("click"); jQuery("#anchor2").die("click");
// Test binding the same handler to multiple points
var called = 0;
function callback(){ called++; return false; }
jQuery("#nothiddendiv").live("click", callback);
jQuery("#anchor2").live("click", callback);
jQuery("#nothiddendiv").trigger("click");
equals( called, 1, "Verify that only one click occurred." );
jQuery("#anchor2").trigger("click");
equals( called, 2, "Verify that only one click occurred." );
// Make sure that only one callback is removed
jQuery("#anchor2").die("click", callback);
jQuery("#nothiddendiv").trigger("click");
equals( called, 3, "Verify that only one click occurred." );
jQuery("#anchor2").trigger("click");
equals( called, 3, "Verify that only one click occurred." );
// Cleanup
jQuery("#nothiddendiv").die("click", callback);
}); });
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment