Making sure that you can bind multiple toggles to a single element without problems. Thanks to 'Mr Speaker' for the original patch. Fixes #5274.
This commit is contained in:
parent
4b55e94d08
commit
5cb1163469
2 changed files with 20 additions and 3 deletions
|
@ -693,13 +693,14 @@ jQuery.fn.extend({
|
||||||
|
|
||||||
return this.click( jQuery.event.proxy( fn, function( event ) {
|
return this.click( jQuery.event.proxy( fn, function( event ) {
|
||||||
// Figure out which function to execute
|
// Figure out which function to execute
|
||||||
this.lastToggle = ( this.lastToggle || 0 ) % i;
|
var lastToggle = ( jQuery.data( this, 'lastToggle' + fn.guid ) || 0 ) % i;
|
||||||
|
jQuery.data( this, 'lastToggle' + fn.guid, lastToggle + 1 );
|
||||||
|
|
||||||
// Make sure that clicks stop
|
// Make sure that clicks stop
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// and execute the function
|
// and execute the function
|
||||||
return args[ this.lastToggle++ ].apply( this, arguments ) || false;
|
return args[ lastToggle ].apply( this, arguments ) || false;
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -532,7 +532,7 @@ test("jQuery.Event.currentTarget", function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
test("toggle(Function, Function, ...)", function() {
|
test("toggle(Function, Function, ...)", function() {
|
||||||
expect(11);
|
expect(16);
|
||||||
|
|
||||||
var count = 0,
|
var count = 0,
|
||||||
fn1 = function(e) { count++; },
|
fn1 = function(e) { count++; },
|
||||||
|
@ -585,6 +585,22 @@ test("toggle(Function, Function, ...)", function() {
|
||||||
$div.unbind('click',fns[0]);
|
$div.unbind('click',fns[0]);
|
||||||
var data = jQuery.data( $div[0], 'events' );
|
var data = jQuery.data( $div[0], 'events' );
|
||||||
ok( !data, "Unbinding one function from toggle unbinds them all");
|
ok( !data, "Unbinding one function from toggle unbinds them all");
|
||||||
|
|
||||||
|
// Test Multi-Toggles
|
||||||
|
var a = [], b = [];
|
||||||
|
$div = jQuery("<div/>");
|
||||||
|
$div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
|
||||||
|
$div.click();
|
||||||
|
same( a, [1], "Check that a click worked." );
|
||||||
|
|
||||||
|
$div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
|
||||||
|
$div.click();
|
||||||
|
same( a, [1,2], "Check that a click worked with a second toggle." );
|
||||||
|
same( b, [1], "Check that a click worked with a second toggle." );
|
||||||
|
|
||||||
|
$div.click();
|
||||||
|
same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
|
||||||
|
same( b, [1,2], "Check that a click worked with a second toggle, second click." );
|
||||||
});
|
});
|
||||||
|
|
||||||
test(".live()/.die()", function() {
|
test(".live()/.die()", function() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue