public
Last active

1.4/1.4.1 and 1.4.2+ compatible event special .add method

  • Download Gist
jquery.event.special.foo.add.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// 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 );
};
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.