Created
September 16, 2011 17:00
-
-
Save billymoon/1222547 to your computer and use it in GitHub Desktop.
explaining problems with jQUery hooks
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
$.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