Side navigation
Ticket #2249: one+diff+toggle.diff
File one+diff+toggle.diff, 2.8 KB (added by flesler, March 29, 2008 08:58PM UTC)
$.proxy moved to $.event.proxy, merged with the 'toggle-with-n-functions' feature. proxy is implemented in toggle as well.
Index: src/event.js
===================================================================
--- src/event.js (revision 5143)
+++ src/event.js (working copy)
@@ -26,16 +26,13 @@
var fn = handler;
// Create unique handler function, wrapped around original handler
- handler = function() {
+ handler = this.proxy( fn, function() {
// Pass arguments and context to original handler
return fn.apply(this, arguments);
- };
+ });
// Store data in unique handler
handler.data = data;
-
- // Set the guid of unique handler to the same of original handler, so it can be removed
- handler.guid = fn.guid;
}
// Init the element's event structure
@@ -336,6 +333,12 @@
return event;
},
+ proxy: function( fn, proxy ){
+ // Set the guid of unique handler to the same of original handler, so it can be removed
+ proxy.guid = fn.guid = fn.guid || this.guid++;
+ return proxy;//so proxy can be declared as an argument
+ },
+
special: {
ready: {
setup: function() {
@@ -401,11 +404,12 @@
},
one: function( type, data, fn ) {
+ var one = jQuery.event.proxy( fn || data, function(event) {
+ jQuery(this).unbind(event, one);
+ return (fn || data).apply( this, arguments);
+ });
return this.each(function(){
- jQuery.event.add( this, type, function(event) {
- jQuery(this).unbind(event);
- return (fn || data).apply( this, arguments);
- }, fn && data);
+ jQuery.event.add( this, type, one, fn && data);
});
},
@@ -427,20 +431,24 @@
return undefined;
},
- toggle: function() {
+ toggle: function( fn ) {
// Save reference to arguments for access in closure
- var args = arguments;
+ var args = arguments, i=args.length;
- return this.click(function(event) {
+ //link all the functions, so any of them can unbind this click handler
+ while( --i )
+ jQuery.event.proxy( fn, args[i] );
+
+ return this.click(jQuery.event.proxy( fn, function(event) {
// Figure out which function to execute
- this.lastToggle = 0 == this.lastToggle ? 1 : 0;
+ this.lastToggle = (this.lastToggle||0) % args.length
// Make sure that clicks stop
event.preventDefault();
// and execute the function
- return args[this.lastToggle].apply( this, arguments ) || false;
- });
+ return args[this.lastToggle++].apply( this, arguments ) || false;
+ }));
},
hover: function(fnOver, fnOut) {
Index: src/fx.js
===================================================================
--- src/fx.js (revision 5143)
+++ src/fx.js (working copy)
@@ -35,7 +35,7 @@
toggle: function( fn, fn2 ){
return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
- this._toggle( fn, fn2 ) :
+ this._toggle.apply( this, arguments ) :
fn ?
this.animate({
height: "toggle", width: "toggle", opacity: "toggle"
Download in other formats:
Original Format
File one+diff+toggle.diff, 2.8 KB (added by flesler, March 29, 2008 08:58PM UTC)
$.proxy moved to $.event.proxy, merged with the 'toggle-with-n-functions' feature. proxy is implemented in toggle as well.
Index: src/event.js
===================================================================
--- src/event.js (revision 5143)
+++ src/event.js (working copy)
@@ -26,16 +26,13 @@
var fn = handler;
// Create unique handler function, wrapped around original handler
- handler = function() {
+ handler = this.proxy( fn, function() {
// Pass arguments and context to original handler
return fn.apply(this, arguments);
- };
+ });
// Store data in unique handler
handler.data = data;
-
- // Set the guid of unique handler to the same of original handler, so it can be removed
- handler.guid = fn.guid;
}
// Init the element's event structure
@@ -336,6 +333,12 @@
return event;
},
+ proxy: function( fn, proxy ){
+ // Set the guid of unique handler to the same of original handler, so it can be removed
+ proxy.guid = fn.guid = fn.guid || this.guid++;
+ return proxy;//so proxy can be declared as an argument
+ },
+
special: {
ready: {
setup: function() {
@@ -401,11 +404,12 @@
},
one: function( type, data, fn ) {
+ var one = jQuery.event.proxy( fn || data, function(event) {
+ jQuery(this).unbind(event, one);
+ return (fn || data).apply( this, arguments);
+ });
return this.each(function(){
- jQuery.event.add( this, type, function(event) {
- jQuery(this).unbind(event);
- return (fn || data).apply( this, arguments);
- }, fn && data);
+ jQuery.event.add( this, type, one, fn && data);
});
},
@@ -427,20 +431,24 @@
return undefined;
},
- toggle: function() {
+ toggle: function( fn ) {
// Save reference to arguments for access in closure
- var args = arguments;
+ var args = arguments, i=args.length;
- return this.click(function(event) {
+ //link all the functions, so any of them can unbind this click handler
+ while( --i )
+ jQuery.event.proxy( fn, args[i] );
+
+ return this.click(jQuery.event.proxy( fn, function(event) {
// Figure out which function to execute
- this.lastToggle = 0 == this.lastToggle ? 1 : 0;
+ this.lastToggle = (this.lastToggle||0) % args.length
// Make sure that clicks stop
event.preventDefault();
// and execute the function
- return args[this.lastToggle].apply( this, arguments ) || false;
- });
+ return args[this.lastToggle++].apply( this, arguments ) || false;
+ }));
},
hover: function(fnOver, fnOut) {
Index: src/fx.js
===================================================================
--- src/fx.js (revision 5143)
+++ src/fx.js (working copy)
@@ -35,7 +35,7 @@
toggle: function( fn, fn2 ){
return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
- this._toggle( fn, fn2 ) :
+ this._toggle.apply( this, arguments ) :
fn ?
this.animate({
height: "toggle", width: "toggle", opacity: "toggle"