jQuery hook, event message passing, callback function

Do you like Drupal’s hook functionality?
Maybe this jQuery extension could serve as a way to replicate that that functionality. Currently I use the extension below to allow the extension of existing libraries I’ve written.

Let’s say in your library you write :

  if (confirmed){
    $('#css-id').hide(
      function(){
         $.hookExecute('css_id_hidden');
      }
    );
  }

One function in a new libarary could write

  function css_id_hidden(){
    alert('Hidden!');
  }

And a different developer

  function css_id_hidden(){
    $('#other-css-id').html('The area is hidden.');
  }

Here’s the code:

/**
 * a jquery hook function
 * 
 * lets a current js function call alternate functions
 * 
 * @todo mimic drupal hook system
 * 
 * @author Jonathan Hendler (jonathan at civicactions dot com)
 * @license AGPL http://www.affero.org/oagpl.html
 * @version 0.1.0
 * 
 *  hookExecute:
 *  
 */
jQuery.extend({ 
    hookExecute: function (function_name){
        //potential security issue
        if (eval ('typeof '+function_name+'=="function"')){
            eval(function_name+'()');
        }else{
            //debug
        }
    }
});
2017-03-31T06:20:54+00:00 Categories: Drupal|

About the Author: