X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fevent.js;h=b50a1ec4e0249c07c0f42f7cd2be81f890225e66;hb=148fb7ba8e992dd70c64cdc6a1c6f643fd1ba160;hp=afe92af595f05ea4d399cf828f434ed22e8d6b77;hpb=3ec2f1aef6b137d0f639e2fc53f95352d24b9d90;p=jquery.git diff --git a/src/event.js b/src/event.js index afe92af..b50a1ec 100644 --- a/src/event.js +++ b/src/event.js @@ -37,13 +37,20 @@ jQuery.event = { // Init the element's event structure var events = jQuery.data( elem, "events" ) || jQuery.data( elem, "events", {} ), - handle = jQuery.data( elem, "handle" ) || jQuery.data( elem, "handle", function eventHandle() { + handle = jQuery.data( elem, "handle" ), eventHandle; + + if ( !handle ) { + eventHandle = function() { // Handle the second event of a trigger and when // an event is called after a page has unloaded return typeof jQuery !== "undefined" && !jQuery.event.triggered ? jQuery.event.handle.apply( eventHandle.elem, arguments ) : undefined; - }); + }; + + handle = jQuery.data( elem, "handle", eventHandle ); + } + // Add elem as a property of the handle function // This is to prevent a memory leak with non-native // event in IE. @@ -680,7 +687,7 @@ function trigger( type, elem, args ) { if ( !jQuery.support.focusBubbles ) { jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){ - event.special[ orig ] = { + jQuery.event.special[ orig ] = { setup: function() { jQuery.event.add( this, fix, ieHandler ); }, @@ -697,13 +704,12 @@ jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){ } -jQuery.fn.extend({ - // TODO: make bind(), unbind() and one() DRY! - bind: function( type, data, fn, thisObject ) { +jQuery.each(["bind", "one"], function(i, name) { + jQuery.fn[ name ] = function( type, data, fn, thisObject ) { // Handle object literals if ( typeof type === "object" ) { for ( var key in type ) { - this.bind(key, data, type[key], fn); + this[ name ](key, data, type[key], fn); } return this; } @@ -714,35 +720,17 @@ jQuery.fn.extend({ data = undefined; } fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject ); - return type === "unload" ? this.one(type, data, fn, thisObject) : this.each(function() { - jQuery.event.add( this, type, fn, data ); - }); - }, - - one: function( type, data, fn, thisObject ) { - // Handle object literals - if ( typeof type === "object" ) { - for ( var key in type ) { - this.one(key, data, type[key], fn); - } - return this; - } - - if ( jQuery.isFunction( data ) ) { - thisObject = fn; - fn = data; - data = undefined; - } - fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject ); - var one = jQuery.event.proxy( fn, function( event ) { - jQuery( this ).unbind( event, one ); + var handler = name == "one" ? jQuery.event.proxy( fn, function( event ) { + jQuery( this ).unbind( event, handler ); return fn.apply( this, arguments ); + }) : fn; + return type === "unload" ? this.one(type, data, handler, thisObject) : this.each(function() { + jQuery.event.add( this, type, handler, data ); }); - return this.each(function() { - jQuery.event.add( this, type, one, data ); - }); - }, + }; +}); +jQuery.fn.extend({ unbind: function( type, fn ) { // Handle object literals if ( typeof type === "object" && !type.preventDefault ) { @@ -751,12 +739,11 @@ jQuery.fn.extend({ } return this; } - + return this.each(function() { jQuery.event.remove( this, type, fn ); }); }, - trigger: function( type, data ) { return this.each(function() { jQuery.event.trigger( type, data, this ); @@ -885,6 +872,10 @@ jQuery.each( ("blur focus load resize scroll unload click dblclick " + jQuery.fn[ name ] = function( fn ) { return fn ? this.bind( name, fn ) : this.trigger( name ); }; + + if ( jQuery.fnAttr ) { + jQuery.fnAttr[ name ] = true; + } }); // Prevent memory leaks in IE @@ -892,11 +883,13 @@ jQuery.each( ("blur focus load resize scroll unload click dblclick " + // More info: // - http://isaacschlueter.com/2006/10/msie-memory-leaks/ /*@cc_on -jQuery( window ).bind( 'unload', function() { +window.attachEvent("onunload", function() { for ( var id in jQuery.cache ) { - // Skip the window - if ( id != 1 && jQuery.cache[ id ].handle ) { - jQuery.event.remove( jQuery.cache[ id ].handle.elem ); + if ( jQuery.cache[ id ].handle ) { + // Try/Catch is to handle iframes being unloaded, see #4280 + try { + jQuery.event.remove( jQuery.cache[ id ].handle.elem ); + } catch(e) {} } } });