added a couple missing semicolons and fixed a typo in the comments
[jquery.git] / src / event.js
index a32f033..b50a1ec 100644 (file)
@@ -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,33 +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 ) {
-               // Handle object literals
-               if ( typeof type === "object" ) {
-                       for ( var key in type ) {
-                               this.bind(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 );
-               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 ) {
+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.one(key, data, type[key], fn);
+                               this[ name ](key, data, type[key], fn);
                        }
                        return this;
                }
@@ -734,15 +720,17 @@ jQuery.fn.extend({
                        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 );
@@ -896,10 +883,9 @@ 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 ) {
+               if ( jQuery.cache[ id ].handle ) {
                        // Try/Catch is to handle iframes being unloaded, see #4280
                        try {
                                jQuery.event.remove( jQuery.cache[ id ].handle.elem );