Skip to content

Instantly share code, notes, and snippets.

@billymoon
Created September 16, 2011 17:00
Show Gist options
  • Save billymoon/1222547 to your computer and use it in GitHub Desktop.
Save billymoon/1222547 to your computer and use it in GitHub Desktop.
explaining problems with jQUery hooks
$.hook = function (fns){
fns = typeof fns === 'string' ? fns.split(' ') : $.makeArray(fns);
jQuery.each( fns, function (i, method) {
var old = $.fn[ method ];
if( old && !old.__hookold ){
$.fn[ method ] = function(){
this.triggerHandler('onbefore'+method);
this.triggerHandler('on'+method);
var ret = old.apply(this, arguments);
this.triggerHandler('onafter'+method);
return ret;
};
$.fn[ method ].__hookold = old;
};
});
};
$.hook('animate')
// have to bind it to all objects using '*'
$('*').bind('onbeforeanimate',function(){ console.log('hooked up') } )
// works here
$('h1').css({border:'1px solid silver'}).animate({borderWidth:10})
$('<h2 />').text('whatever')
// does not work here
$('h2').animate({width:200})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment