Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Remove the .bind(name, fn, thisObject) and promote jQuery.event.proxy…

…() to jQuery.proxy() as alternative to handling scoping on callbacks. Fixes #5736.
  • Loading branch information...
commit 66975de2d249643779e2b3daad0457f7f5f92508 1 parent a00e63e
John Resig authored December 31, 2009
60  src/event.js
@@ -35,7 +35,7 @@ jQuery.event = {
35 35
 			var fn = handler;
36 36
 
37 37
 			// Create unique handler function, wrapped around original handler
38  
-			handler = this.proxy( fn );
  38
+			handler = jQuery.proxy( fn );
39 39
 
40 40
 			// Store data in unique handler
41 41
 			handler.data = data;
@@ -405,24 +405,6 @@ jQuery.event = {
405 405
 		return event;
406 406
 	},
407 407
 
408  
-	proxy: function( fn, proxy, thisObject ) {
409  
-		if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
410  
-			thisObject = proxy;
411  
-			proxy = undefined;
412  
-		}
413  
-
414  
-		// FIXME: Should proxy be redefined to be applied with thisObject if defined?
415  
-		proxy = proxy || function() {
416  
-			return fn.apply( thisObject !== undefined ? thisObject : this, arguments );
417  
-		};
418  
-
419  
-		// Set the guid of unique handler to the same of original handler, so it can be removed
420  
-		proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
421  
-
422  
-		// So proxy can be declared as an argument
423  
-		return proxy;
424  
-	},
425  
-
426 408
 	special: {
427 409
 		ready: {
428 410
 			// Make sure the ready event is setup
@@ -474,6 +456,24 @@ jQuery.event = {
474 456
 	}
475 457
 };
476 458
 
  459
+jQuery.event.proxy = jQuery.proxy = function( fn, proxy, thisObject ) {
  460
+	if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
  461
+		thisObject = proxy;
  462
+		proxy = undefined;
  463
+	}
  464
+
  465
+	// FIXME: Should proxy be redefined to be applied with thisObject if defined?
  466
+	proxy = proxy || function() {
  467
+		return fn.apply( thisObject !== undefined ? thisObject : this, arguments );
  468
+	};
  469
+
  470
+	// Set the guid of unique handler to the same of original handler, so it can be removed
  471
+	proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.event.guid++;
  472
+
  473
+	// So proxy can be declared as an argument
  474
+	return proxy;
  475
+};
  476
+
477 477
 jQuery.Event = function( src ) {
478 478
 	// Allow instantiation without the 'new' keyword
479 479
 	if ( !this.preventDefault ) {
@@ -759,7 +759,7 @@ if ( document.addEventListener ) {
759 759
 }
760 760
 
761 761
 jQuery.each(["bind", "one"], function( i, name ) {
762  
-	jQuery.fn[ name ] = function( type, data, fn, thisObject ) {
  762
+	jQuery.fn[ name ] = function( type, data, fn ) {
763 763
 		// Handle object literals
764 764
 		if ( typeof type === "object" ) {
765 765
 			for ( var key in type ) {
@@ -773,12 +773,13 @@ jQuery.each(["bind", "one"], function( i, name ) {
773 773
 			fn = data;
774 774
 			data = undefined;
775 775
 		}
776  
-		fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
777  
-		var handler = name === "one" ? jQuery.event.proxy( fn, function( event ) {
  776
+
  777
+		var handler = name === "one" ? jQuery.proxy( fn, function( event ) {
778 778
 			jQuery( this ).unbind( event, handler );
779 779
 			return fn.apply( this, arguments );
780 780
 		}) : fn;
781  
-		return type === "unload" ? this.one(type, data, handler, thisObject) : this.each(function() {
  781
+
  782
+		return type === "unload" ? this.one(type, data, handler) : this.each(function() {
782 783
 			jQuery.event.add( this, type, handler, data );
783 784
 		});
784 785
 	};
@@ -820,10 +821,10 @@ jQuery.fn.extend({
820 821
 
821 822
 		// link all the functions, so any of them can unbind this click handler
822 823
 		while ( i < args.length ) {
823  
-			jQuery.event.proxy( fn, args[ i++ ] );
  824
+			jQuery.proxy( fn, args[ i++ ] );
824 825
 		}
825 826
 
826  
-		return this.click( jQuery.event.proxy( fn, function( event ) {
  827
+		return this.click( jQuery.proxy( fn, function( event ) {
827 828
 			// Figure out which function to execute
828 829
 			var lastToggle = ( jQuery.data( this, "lastToggle" + fn.guid ) || 0 ) % i;
829 830
 			jQuery.data( this, "lastToggle" + fn.guid, lastToggle + 1 );
@@ -840,17 +841,16 @@ jQuery.fn.extend({
840 841
 		return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
841 842
 	},
842 843
 
843  
-	live: function( type, data, fn, thisObject ) {
  844
+	live: function( type, data, fn ) {
844 845
 		if ( jQuery.isFunction( data ) ) {
845  
-			if ( fn !== undefined ) {
846  
-				thisObject = fn;
847  
-			}
848 846
 			fn = data;
849 847
 			data = undefined;
850 848
 		}
  849
+
851 850
 		jQuery( this.context ).bind( liveConvert( type, this.selector ), {
852 851
 			data: data, selector: this.selector, live: type
853  
-		}, fn, thisObject );
  852
+		}, fn );
  853
+
854 854
 		return this;
855 855
 	},
856 856
 
10  test/unit/event.js
@@ -232,8 +232,8 @@ test("bind(), with different this object", function() {
232 232
 		};
233 233
 	
234 234
 	jQuery("#firstp")
235  
-		.bind("click", handler1, thisObject).click().unbind("click", handler1)
236  
-		.bind("click", data, handler2, thisObject).click().unbind("click", handler2);
  235
+		.bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
  236
+		.bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
237 237
 
238 238
 	ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
239 239
 });
@@ -706,15 +706,15 @@ test(".live()/.die()", function() {
706 706
 	jQuery("#foo").trigger("click", true).die("click");
707 707
 
708 708
 	// Test binding with different this object
709  
-	jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
  709
+	jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
710 710
 	jQuery("#foo").trigger("click").die("click");
711 711
 
712 712
 	// Test binding with different this object, event data, and trigger data
713  
-	jQuery("#foo").live("click", true, function(e, data){
  713
+	jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
714 714
 		equals( e.data, true, "live with with different this object, event data, and trigger data" );
715 715
 		equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
716 716
 		equals( data, true, "live with with different this object, event data, and trigger data")
717  
-	}, { foo: "bar" });
  717
+	}, { foo: "bar" }));
718 718
 	jQuery("#foo").trigger("click", true).die("click");
719 719
 
720 720
 	// Verify that return false prevents default action

1 note on commit 66975de

Aaron Heckmann

Is there any reason to leave jQuery.event.proxy here? I don't see anywhere that uses it now.

John Resig

As I commented on jquery-dev I'm leaving it in so that we don't break compatibility with plugins.

Please sign in to comment.
Something went wrong with that request. Please try again.