software-mariodiana
12/12/2014 - 6:50 PM

Given a button ("element"), wrap the function currently bound to its onclick method by using the Proxy pattern.

Given a button ("element"), wrap the function currently bound to its onclick method by using the Proxy pattern.

/**
 * Decorate the button element's onclick function with custom behavior.
 */
var wrapButtonOnClickFunctionForElement = function(element) {
    // http://api.jquery.com/Types/#Proxy_Pattern
    (function() {
        var proxiedFunction = element.onclick;
        element.onclick = function() {
            // Add custom functionality...
            console.log('## Wrapping onclick: ' + new Date());
            console.log('##     this: ' + this);
            console.log('##     args: ' + arguments);
            // Return the original function to the caller.
            return proxiedFunction.apply(this, arguments);
        };
    })();
};