Triggered event handlers are only executed once now.
[jquery.git] / src / event / event.js
index 4a63316..a948bfc 100644 (file)
@@ -74,28 +74,34 @@ jQuery.event = {
                data = jQuery.makeArray(data || []);
 
                // Handle a global trigger
-               if ( !element ) {
-                       var g = this.global[type];
-                       if ( g )
-                               for ( var i = 0, gl = g.length; i < gl; i++ )
-                                       this.trigger( type, data, g[i] );
+               if ( !element )
+                       jQuery.each( this.global[type] || [], function(){
+                               jQuery.event.trigger( type, data, this );
+                       });
 
                // Handle triggering a single element
-               } else if ( element["on" + type] ) {
-                       if ( element[ type ] && element[ type ].constructor == Function )
-                               element[ type ]();
-                       else {
-                               // Pass along a fake event
-                               data.unshift( this.fix({ type: type, target: element }) );
+               else if ( element["on" + type] ) {
+                       // Pass along a fake event
+                       data.unshift( this.fix({ type: type, target: element }) );
        
-                               // Trigger the event
-                               element["on" + type].apply( element, data );
+                       // Trigger the event
+                       var val = element["on" + type].apply( element, data );
+
+                       if ( val !== false && jQuery.isFunction( element[ type ] ) ) {
+                               this.triggered = true;
+                               element[ type ]();
                        }
                }
        },
 
        handle: function(event) {
-               if ( typeof jQuery == "undefined" ) return false;
+               if ( typeof jQuery == "undefined" ) return;
+
+               // Handle the second event of a trigger
+               if ( jQuery.event.triggered ) {
+                       jQuery.event.triggered = false;
+                       return;
+               }
 
                // Empty object is for triggered events with no data
                event = jQuery.event.fix( event || window.event || {} ); 
@@ -133,7 +139,7 @@ jQuery.event = {
                        event.target = event.srcElement;
 
                // Calculate pageX/Y if missing and clientX/Y available
-               if ( typeof event.pageX == "undefined" && typeof event.clientX != "undefined" ) {
+               if ( event.pageX == undefined && event.clientX != undefined ) {
                        var e = document.documentElement, b = document.body;
                        event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
                        event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
@@ -408,6 +414,10 @@ jQuery.fn.extend({
         * and attaching a function to that. By using this method, your bound Function 
         * will be called the instant the DOM is ready to be read and manipulated, 
         * which is exactly what 99.99% of all Javascript code needs to run.
+        *
+        * There is one argument passed to the ready event handler: A reference to
+        * the jQuery function. You can name that argument whatever you like, and
+        * can therefore stick with the $ alias without risc of naming collisions.
         * 
         * Please ensure you have no code in your &lt;body&gt; onload event handler, 
         * otherwise $(document).ready() may not fire.
@@ -417,21 +427,30 @@ jQuery.fn.extend({
         *
         * @example $(document).ready(function(){ Your code here... });
         *
+        * @example jQuery(function($) {
+        *   // Your code using failsafe $ alias here...
+        * });
+        * @desc Uses both the shortcut for $(document).ready() and the argument
+        * to write failsafe jQuery code using the $ alias, without relying on the
+        * global alias.
+        *
         * @name ready
         * @type jQuery
         * @param Function fn The function to be executed when the DOM is ready.
         * @cat Events
+        * @see $.noConflict()
+        * @see $(Function)
         */
        ready: function(f) {
                // If the DOM is already ready
                if ( jQuery.isReady )
                        // Execute the function immediately
-                       f.apply( document );
+                       f.apply( document, [jQuery] );
                        
                // Otherwise, remember the function for later
                else {
                        // Add the function to the wait list
-                       jQuery.readyList.push( f );
+                       jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } );
                }
        
                return this;
@@ -455,8 +474,9 @@ jQuery.extend({
                        // If there are functions bound, to execute
                        if ( jQuery.readyList ) {
                                // Execute all of them
-                               for ( var i = 0; i < jQuery.readyList.length; i++ )
-                                       jQuery.readyList[i].apply( document );
+                               jQuery.each( jQuery.readyList, function(){
+                                       this.apply( document );
+                               });
                                
                                // Reset the list of functions
                                jQuery.readyList = null;