Created
August 21, 2013 06:41
-
-
Save peterwilsoncc/6291028 to your computer and use it in GitHub Desktop.
add_action, do_action in Javascript
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
function add_action( action_name, callback, priority ) { | |
if ( ! priority ) { | |
priority = 10; | |
} | |
if ( priority > 100 ) { | |
priority = 100; | |
} | |
if ( priority < 0 ) { | |
priority = 0; | |
} | |
if ( typeof actions[action_name] == 'undefined' ) { | |
actions[action_name] = []; | |
} | |
if ( typeof actions[action_name][priority] == 'undefined' ) { | |
actions[action_name][priority] = [] | |
} | |
actions[action_name][priority].push( callback ); | |
} | |
function do_action() { | |
if ( arguments.length == 0 ) { | |
return; | |
} | |
var args_accepted = Array.prototype.slice.call(arguments), | |
action_name = args_accepted.shift(), | |
_this = this, | |
i, | |
ilen, | |
j, | |
jlen; | |
if ( typeof actions[action_name] == 'undefined' ) { | |
return; | |
} | |
for ( i = 0, ilen=100; i<=ilen; i++ ) { | |
if ( actions[action_name][i] ) { | |
for ( j = 0, jlen=actions[action_name][i].length; j<jlen; j++ ) { | |
actions[action_name][i][j].apply( _this, args_accepted ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Peter and @BronsonQuick refer to http://core.trac.wordpress.org/ticket/21170