Some small optimizations to the event module. jQuery.event.trigger over 200% faster in IE and less code. Thanks in large to Ariel Flesler.
This commit is contained in:
parent
9de35ce3e7
commit
6d28ebff85
56
src/event.js
56
src/event.js
|
@ -41,17 +41,10 @@ jQuery.event = {
|
||||||
// Init the element's event structure
|
// Init the element's event structure
|
||||||
var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}),
|
var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}),
|
||||||
handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
|
handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
|
||||||
// returned undefined or false
|
|
||||||
var val;
|
|
||||||
|
|
||||||
// Handle the second event of a trigger and when
|
// Handle the second event of a trigger and when
|
||||||
// an event is called after a page has unloaded
|
// an event is called after a page has unloaded
|
||||||
if ( typeof jQuery == "undefined" || jQuery.event.triggered )
|
if ( typeof jQuery != "undefined" && !jQuery.event.triggered )
|
||||||
return val;
|
return jQuery.event.handle.apply(arguments.callee.elem, arguments);
|
||||||
|
|
||||||
val = jQuery.event.handle.apply(arguments.callee.elem, arguments);
|
|
||||||
|
|
||||||
return val;
|
|
||||||
});
|
});
|
||||||
// Add elem as a property of the handle function
|
// Add elem as a property of the handle function
|
||||||
// This is to prevent a memory leak with non-native
|
// This is to prevent a memory leak with non-native
|
||||||
|
@ -199,9 +192,10 @@ jQuery.event = {
|
||||||
if ( exclusive )
|
if ( exclusive )
|
||||||
data[0].exclusive = true;
|
data[0].exclusive = true;
|
||||||
|
|
||||||
// Trigger the event
|
// Trigger the event, it is assumed that "handle" is a function
|
||||||
if ( jQuery.isFunction( jQuery.data(elem, "handle") ) )
|
var handle = jQuery.data(elem, "handle");
|
||||||
val = jQuery.data(elem, "handle").apply( elem, data );
|
if ( handle )
|
||||||
|
val = handle.apply( elem, data );
|
||||||
|
|
||||||
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
|
// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
|
||||||
if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
|
if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
|
||||||
|
@ -237,39 +231,37 @@ jQuery.event = {
|
||||||
|
|
||||||
handle: function(event) {
|
handle: function(event) {
|
||||||
// returned undefined or false
|
// returned undefined or false
|
||||||
var val;
|
var val, namespace, all, handlers;
|
||||||
|
|
||||||
// Empty object is for triggered events with no data
|
event = arguments[0] = jQuery.event.fix( event || window.event );
|
||||||
event = jQuery.event.fix( event || window.event || {} );
|
|
||||||
|
|
||||||
// Namespaced event handlers
|
// Namespaced event handlers
|
||||||
var parts = event.type.split(".");
|
namespace = event.type.split(".");
|
||||||
event.type = parts[0];
|
event.type = namespace[0];
|
||||||
|
namespace = namespace[1];
|
||||||
|
all = !namespace && !event.exclusive; //cache this now, all = true means, any handler
|
||||||
|
|
||||||
var handlers = jQuery.data(this, "events") && jQuery.data(this, "events")[event.type], args = Array.prototype.slice.call( arguments, 1 );
|
handlers = ( jQuery.data(this, "events") || {} )[event.type];
|
||||||
args.unshift( event );
|
|
||||||
|
|
||||||
for ( var j in handlers ) {
|
for ( var j in handlers ) {
|
||||||
var handler = handlers[j];
|
var handler = handlers[j];
|
||||||
// Pass in a reference to the handler function itself
|
|
||||||
// So that we can later remove it
|
|
||||||
args[0].handler = handler;
|
|
||||||
args[0].data = handler.data;
|
|
||||||
|
|
||||||
// Filter the functions by class
|
// Filter the functions by class
|
||||||
if ( !parts[1] && !event.exclusive || handler.type == parts[1] ) {
|
if ( all || handler.type == namespace ) {
|
||||||
var ret = handler.apply( this, args );
|
// Pass in a reference to the handler function itself
|
||||||
|
// So that we can later remove it
|
||||||
|
event.handler = handler;
|
||||||
|
event.data = handler.data;
|
||||||
|
|
||||||
if ( val !== false )
|
val = handler.apply( this, arguments );
|
||||||
val = ret;
|
|
||||||
|
|
||||||
if ( ret === false ) {
|
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( val === false ) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up added properties in IE to prevent memory leak
|
// Clean up added properties in IE to prevent memory leak
|
||||||
if (jQuery.browser.msie)
|
if (jQuery.browser.msie)
|
||||||
event.target = event.preventDefault = event.stopPropagation =
|
event.target = event.preventDefault = event.stopPropagation =
|
||||||
|
|
Loading…
Reference in a new issue