use jquery for dblclick handler to access event.target
[jquery.git] / src / event / event.js
index 77a4046..623bcf3 100644 (file)
@@ -41,7 +41,17 @@ jQuery.event = {
                
                if (!element.$handle)
                        element.$handle = function() {
-                               jQuery.event.handle.apply(element, arguments);
+                               // returned undefined or false
+                               var val;
+
+                               // Handle the second event of a trigger and when
+                               // an event is called after a page has unloaded
+                               if ( typeof jQuery == "undefined" || jQuery.event.triggered )
+                                 return val;
+                               
+                               val = jQuery.event.handle.apply(element, arguments);
+                               
+                               return val;
                        };
 
                // Get the current list of functions bound to this event
@@ -64,7 +74,9 @@ jQuery.event = {
                // Remember the function in a global list (for triggering)
                if (!this.global[type])
                        this.global[type] = [];
-               this.global[type].push( element );
+               // Only add the element to the global list once
+               if (jQuery.inArray(element, this.global[type]) == -1)
+                       this.global[type].push( element );
        },
 
        guid: 1,
@@ -72,7 +84,7 @@ jQuery.event = {
 
        // Detach an event or set of events from an element
        remove: function(element, type, handler) {
-               var events = element.$events, ret;
+               var events = element.$events, ret, index;
 
                if ( events ) {
                        // type is actually an event object here
@@ -104,6 +116,10 @@ jQuery.event = {
                                                element.detachEvent("on" + type, element.$handle);
                                        ret = null;
                                        delete events[type];
+                                       
+                                       // Remove element from the global event type cache
+                                       while ( this.global[type] && ( (index = jQuery.inArray(element, this.global[type])) >= 0 ) )
+                                               delete this.global[type][index];
                                }
                        }
 
@@ -132,7 +148,7 @@ jQuery.event = {
                        data.unshift( this.fix({ type: type, target: element }) );
 
                        // Trigger the event
-                       if ( (val = this.handle.apply( element, data )) !== false )
+                       if ( (val = element.$handle.apply( element, data )) !== false )
                                this.triggered = true;
 
                        if ( fn && val !== false && !jQuery.nodeName(element, 'a') )
@@ -145,11 +161,6 @@ jQuery.event = {
        handle: function(event) {
                // returned undefined or false
                var val;
-               
-               // Handle the second event of a trigger and when
-               // an event is called after a page has unloaded
-               if ( typeof jQuery == "undefined" || jQuery.event.triggered )
-                 return val;
 
                // Empty object is for triggered events with no data
                event = jQuery.event.fix( event || window.event || {} ); 
@@ -250,6 +261,9 @@ jQuery.fn.extend({
         * data as the second parameter (and the handler function as the third), see 
         * second example.
         *
+        * Calling bind with an event type of "unload" will automatically
+        * use the one method instead of bind to prevent memory leaks.
+        *
         * @example $("p").bind("click", function(){
         *   alert( $(this).text() );
         * });
@@ -286,7 +300,7 @@ jQuery.fn.extend({
         * @cat Events
         */
        bind: function( type, data, fn ) {
-               return this.each(function(){
+               return type == "unload" ? this.one(type, data, fn) : this.each(function(){
                        jQuery.event.add( this, type, fn || data, fn && data );
                });
        },