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 (new plugins, use this) | |
$.event.special.foo = { | |
add: function( handleObj ) { | |
// Do something when event is bound here! | |
var old_handler = handleObj.handler; | |
handleObj.handler = function(event) { | |
// handleObj.type (string, 'foo' in this case) | |
// handleObj.data (anything) | |
// handleObj.namespace (string, sorted.namespace.list) | |
// Modify event object here! | |
old_handler.apply( this, arguments ); | |
}; | |
} | |
}; | |
// 1.4, 1.4.1 and 1.4.2 (existing plugins, update for 1.4.2 this way) | |
$.event.special.foo = { | |
add: function( handleObj ) { | |
// Do something when event is bound here! | |
var old_handler; | |
function new_handler(event) { | |
// Modify event object here! | |
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; | |
return new_handler; | |
} else { | |
// 1.4.2+ | |
old_handler = handleObj.handler; | |
handleObj.handler = new_handler; | |
} | |
} | |
}; | |
// 1.4, 1.4.1 (for reference only, the way it used to be) | |
$.event.special.foo = { | |
add: function( handler, data, namespaces ) { | |
// Do something when event is bound here! | |
return function(event) { | |
// handler.type (string, sorted.namespace.list, NOT the event type) | |
// data (anything) | |
// namespaces (sorted array of namespace strings) | |
// Modify event object here! | |
handler.apply( this, arguments ); | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment