Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Created August 21, 2013 06:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterwilsoncc/6291028 to your computer and use it in GitHub Desktop.
Save peterwilsoncc/6291028 to your computer and use it in GitHub Desktop.
add_action, do_action in Javascript
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 );
}
}
}
}
@peterwilsoncc
Copy link
Author

@peterwilsoncc
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment