jQuery.override plugin.
Because sometimes, it just needs to work.

Usage:

Convert an element's inline event into a jQuery .bind() subscription:
$('#button').override('onclick','click');

Replace an element's inline event, with the option to call it:
$('#button').override('onclick', 'click', function(oldFunction, element, arguments) {
   if(newBehavior) {
      alert('new behavior only.');
   } else {
      oldFunction(); //will invoke passing 'this' and 'arguments'      
   }
});

Restore an overridden attribute on an element:
$('#button').override('restore', 'onclick');

Restore the entire element:
$('#button').override('restore');

Wrap a function, with the option to call it:
function doSomeWork(x,y,z) {
   alert('doSomeWork ' + x);
}
//...
doSomeWork = $(document).override(doSomeWork, function(argumentsPassed, oldFunction) {
   if(newBehavior) {
      alert('new behavior only');
   } else {
      oldFunction(); //will invoke passing 'this' and original arguments
   }
});

Subscribe to doSomeWork after it has been overridden, requires $(document).override(...) to have been called:
$(document).override(doSomeWork, 'subscribe', function(event) {
   if(cancelLegacyBehavior) {
      event.cancel = true; //the doSomeWork overridden function will not execute
   }
});

Unsubscribe to doSomeWork:
$(document.override(doSomeWork, 'unsubscribe', function(...)); //unsubscribe from this function
$(document.override(doSomeWork, 'unsubscribe'); //unsubscribe all from this function

Restore doSomeWork:
doSomeWork = $(document).override(doSomeWork, 'restore'); //restore function to original state


Forget Duck Punching. Get me a sledgehammer.

Last edited Nov 14, 2012 at 7:25 AM by aikeru, version 6