Created
February 8, 2010 19:52
-
-
Save cowboy/298508 to your computer and use it in GitHub Desktop.
1.4/1.4.1 and 1.4.2+ compatible event special .add method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1.4.2 | |
$.event.special.foo = { | |
add: function( handleObj ) { | |
// Do something when event is bound here! | |
// Modify event object here! | |
var old_handler = handleObj.handler; | |
handleObj.handler = function(event) { | |
// handleObj.type (string) | |
// handleObj.data (anything) | |
// handleObj.namespace (string.dot.separated) | |
old_handler.apply( this, arguments ); | |
}; | |
} | |
}; | |
// 1.4, 1.4.1 | |
$.event.special.foo = { | |
add: function( handler, data, namespaces ) { | |
// Do something when event is bound here! | |
// Modify event object here! | |
return function(event) { | |
// handler.type (string, needs to be massaged to be useful) | |
// data (anything) | |
// namespaces (sorted array) | |
handler.apply( this, arguments ); | |
}; | |
} | |
}; | |
// 1.4, 1.4.1 and 1.4.2 | |
$.event.special.foo = { | |
add: function( handleObj, data, namespaces ) { | |
// Do something when event is bound here! | |
var old_handler; | |
// Modify event object here! | |
function new_handler(e) { | |
// handleObj.type (string) | |
// handleObj.data (anything) | |
// handleObj.namespace (string.dot.separated) | |
old_handler.apply( this, arguments ); | |
}; | |
// This may seem a little complicated, but it normalizes the special event | |
// .add method between jQuery 1.4/1.4.1 and 1.4.2+ | |
if ( $.isFunction( handleObj ) ) { | |
// 1.4, 1.4.1 | |
old_handler = handleObj; | |
handleObj = { | |
type: old_handler.type.split('.')[0], | |
data: data, | |
namespace: namespaces.join('.') | |
}; | |
return new_handler; | |
} else { | |
// 1.4.2+ | |
old_handler = handleObj.handler; | |
handleObj.handler = new_handler; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment