Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created December 20, 2012 14:31
Show Gist options
  • Save lgedeon/4345601 to your computer and use it in GitHub Desktop.
Save lgedeon/4345601 to your computer and use it in GitHub Desktop.
My first JS Hooks idea. Things got complex shortly after this :)
/* bind to variable */
jQuery(document).ready(function() {
// register a filter and override the functions
jQuery('body').bind('myTrig', function(event, obj) {
obj.valueOf=obj.toSource=obj.toString=function(){return "blue"};
});
// create a variable as an object and pass it to any filters that have been registered
var o = Object('green');
jQuery('body').trigger('myTrig', o);
alert(o); // should return blue
});
/* bind to object */
jQuery(document).ready(function() {
// register a filter
jQuery('body').bind('myTrig', function(event, obj) {
obj.value = "blue"; // set the value of the object instead of returning a value
});
// create an object and pass it to any filters that have been registered
var o = {value: 'green'};
jQuery('body').trigger('myTrig', o);
alert(o); // should return blue
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment