Skip to content

Commit

Permalink
Merge e9890d7 into b5a16ea
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Merge Button committed Aug 4, 2011
2 parents b5a16ea + e9890d7 commit 546339a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/core.js
Expand Up @@ -785,6 +785,8 @@ jQuery.extend({
return fn.apply( context, args.concat( slice.call( arguments ) ) ); return fn.apply( context, args.concat( slice.call( arguments ) ) );
}; };


proxy.context = context;

// 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 || jQuery.guid++; proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;


Expand Down
2 changes: 1 addition & 1 deletion src/event.js
Expand Up @@ -221,7 +221,7 @@ jQuery.event = {
for ( j = pos || 0; j < eventType.length; j++ ) { for ( j = pos || 0; j < eventType.length; j++ ) {
handleObj = eventType[ j ]; handleObj = eventType[ j ];


if ( handler.guid === handleObj.guid ) { if ( handler.guid === handleObj.guid && ( ( handler.context && handler.context === handleObj.handler.context ) || !handler.context ) ) {
// remove the given handler for the given type // remove the given handler for the given type
if ( all || namespace.test( handleObj.namespace ) ) { if ( all || namespace.test( handleObj.namespace ) ) {
if ( pos == null ) { if ( pos == null ) {
Expand Down
44 changes: 44 additions & 0 deletions test/unit/event.js
Expand Up @@ -2209,6 +2209,50 @@ test("custom events with colons (#3533, #8272)", function() {


}); });


test("Unbind proxied functions (#9278)", function() {
expect(4);

var result = [];

function eventHandler() {
result.push(this.name);
}

var eventSource = {};

var firstContext = { name: 'Marx Brothers' };
var secondContext = { name: 'Team Possible' };

var eventHandler$firstContext = jQuery.proxy(eventHandler, firstContext);
var eventHandler$secondContext = jQuery.proxy(eventHandler, secondContext);

// no handlers
jQuery(eventSource).triggerHandler('customEvent');
equals(result.length, 0, 'No handlers bound');

// 3 handlers bound (1 of the first, 2 of the second)
jQuery(eventSource).bind('customEvent', eventHandler$firstContext);
jQuery(eventSource).bind('customEvent', eventHandler$secondContext);
jQuery(eventSource).bind('customEvent', eventHandler$secondContext);
jQuery(eventSource).triggerHandler('customEvent');
ok(result.length === 3 &&
result[0] === firstContext.name &&
result[1] === secondContext.name &&
result[2] === secondContext.name, 'All three proxies get triggered');
result.length = 0;

// unbind second one
jQuery(eventSource).unbind('customEvent', eventHandler$secondContext);
jQuery(eventSource).triggerHandler('customEvent');
ok(result.length === 1 && result[0] === firstContext.name, 'Remaining proxy get triggered');
result.length = 0;

// unbind remaining handler
jQuery(eventSource).unbind('customEvent', eventHandler$firstContext);
jQuery(eventSource).triggerHandler('customEvent');
equals(result.length, 0, 'No handlers remaining');
});

(function(){ (function(){
// This code must be run before DOM ready! // This code must be run before DOM ready!
var notYetReady, noEarlyExecution, var notYetReady, noEarlyExecution,
Expand Down

0 comments on commit 546339a

Please sign in to comment.