See the raw file here: /scripts/jquery-object-events.js
(function($){ /* ----- * @author James Padolsey (http://james.padolsey.com) * ----- * Extends jQuery's event facade by enabling you to * pass just a 'data' object with no corresponding * handler. * E.g. $(elem).bind('click', {data:1}) * ----- * This data object is not treated as data in this * situation. Its property names are treated as * jQuery method names and its property values * are treated as arguments to the associated * jQuery method. * ----- * Example usage: * $(elem).bind('click', { * addClass: 'clicked', * css: { * color: 'red' * } * }); * ----- * This extension is only activated when you pass * a data object *without* a handler function for * an event. If you pass the handler then the object * will be treated as regular data; like before... * ----- */ // Cache necessary methods: var _eventAdd = $.event.add, _eventBind = $.fn.bind, _eventOne = $.fn.one, _eventRemove = $.event.remove, uid = +new Date(); $.fn.bind = function(type, data, fn) { // Need a way to determine whether a handler // is passed; add secret key to data object: if (!fn) { data['__extended' + uid] = true; } // No handler? - Then add an empty one: fn = fn || (!$.isFunction(data) && function(){}); // Bind it as usual: return _eventBind.call(this, type, data, fn); }; $.fn.one = function(type, data, fn) { // (Same as above) if (!fn) { data['__extended' + uid] = true; } fn = fn || (!$.isFunction(data) && function(){}); return _eventOne.call(this, type, data, fn); }; $.event.remove = function(elem, types, handler) { if (handler && !$.isFunction(handler) && handler['__handler' + uid]) { handler = handler['__handler' + uid]; } _eventRemove.call(this, elem, types, handler); }; $.event.add = function(elem, types, handler, data) { // If __extended prop is found, then its assumed // to be a non-data executable object: handler = data['__extended' + uid] ? function() { data['__handler' + uid] = handler; for (var i in data) { // Check whether the prop name is a // jQuery method before firing: $.fn[i] && $.fn[i].apply( $(this), $.isArray(data[i]) ? data[i] : [data[i]] ); } } : handler; // Continue with core functionality: _eventAdd.call(this, elem, types, handler, data); }; })(jQuery);
ALL COPYRIGHT © James Padolsey unless otherwise specified
Go back to the top