Rewrote $.xml a bit, cause the loading message got stuck (didn't hide) after fast...
[jquery.git] / ajax / ajax.js
index d3443cc..fef1e22 100644 (file)
@@ -9,7 +9,6 @@ if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'fun
        };
 }
 
-//
 // Counter for holding the active query's
 $.xmlActive=0;
 
@@ -17,25 +16,11 @@ $.xml = function( type, url, data, ret ) {
        var xml = new XMLHttpRequest();
 
        if ( xml ) {
-               //
-               // Increase the query counter
-               $.xmlActive++;
-
-               //
-               // Show loader if needed
-               if ($.xmlCreate) {
-                       $.xmlCreate();
-               }
-
-               //
                // Open the socket
                xml.open(type || "GET", url, true);
-
-               if ( data ) {
+               if ( data )
                        xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-               }
 
-               //
                // Set header so calling script knows that it's an XMLHttpRequest
                xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
 
@@ -43,29 +28,38 @@ $.xml = function( type, url, data, ret ) {
                 * a bug where XMLHttpReqeuest sends an incorrect Content-length
                 * header. See Mozilla Bugzilla #246651.
                 */
-               if ( xml.overrideMimeType ) {
+               if ( xml.overrideMimeType )
                        xml.setRequestHeader('Connection', 'close');
-               }
 
                xml.onreadystatechange = function() {
-                       if ( xml.readyState == 4 ) {
-                               if ( ret ) { ret(xml); }
+                       // Socket is openend
+                       if ( xml.readyState == 1 ) {
+                               // Increase counter
+                               $.xmlActive++;
+
+                               // Show loader if needed
+                               if ( ($.xmlActive >= 1) && ($.xmlCreate) )
+                                       $.xmlCreate();
+                       }
 
-                               //
+                       // Socket is closed and data is available
+                       if ( xml.readyState == 4 ) {
                                // Decrease counter
                                $.xmlActive--;
 
-                               //
                                // Hide loader if needed
-                               if ($.xmlActive <= 0) {
-                                       if ($.xmlDestroy) {
-                                               $.xmlDestroy();
-                                       }
+                               if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) {
+                                       $.xmlDestroy();
+                                       $.xmlActive = 0
                                }
+
+                               // Process result
+                               if ( ret )
+                                       ret(xml);
                        }
                };
 
-               xml.send(data);
+               xml.send(data)
        }
 };
 
@@ -145,28 +139,23 @@ $.fn.load = function(a,o,f) {
  */
 $.fn.formValues = function() {
        var a = [];
-       this.find("input,textarea,option")
-               .filter(":enabled")
-               .each(function() {
-                       //
-                       // Skip selects with options which aren't selected
-                       if (((this.parentNode.type == 'select-one') || (this.parentNode.type == 'select-multiple')) &&
-                               (!this.selected))
-                               return;
-
-                       //
-                       // Skip radio and checkbox elements which aren't checked
-                       if (((this.type == 'radio') || (this.type == 'checkbox')) &&
-                               (!this.checked))
-                               return;
-
-                       //
-                       // All other elements are valid ;)
-                       var o = {};
-                       o.name = this.name || this.id || this.parentNode.name || this.parentNode.id;
-                       o.value = this.value;
-                       a.push(o);
+       $("input,textarea,option",this).filter(":enabled").each(function(){
+               // Skip selects with options which are not selected
+               if ((this.parentNode.type == 'select-one' || this.parentNode.type == 'select-multiple') && !this.selected) {
+                       return null;
+               }
+
+               // Skip radio and checkbox elements which are not checked
+               if ((this.type == 'radio' || this.type == 'checkbox') && !this.checked) {
+                       return null;
+               }
+
+               // All other elements are valid
+               a.push({
+                       name: this.name || this.id || this.parentNode.name || this.parentNode.id,
+                       value: this.value
                });
+       });
        return a;
 };
 
@@ -179,11 +168,9 @@ $.fn.formValues = function() {
  */
 $.update = function(objElement, strURL, arrValues, fncCallback) {
        $.post(strURL, arrValues, function(strHTML) {
-               //
                // Update the element with the new HTML
                objElement.html(strHTML);
 
-               //
                // Evaluate the scripts
                objElement.html(strHTML).find("script").each(function(){
                        try {
@@ -191,7 +178,6 @@ $.update = function(objElement, strURL, arrValues, fncCallback) {
                        } catch(e) { }
                });
 
-               //
                // Callback handler
                if (fncCallback) { fncCallback(); }
        });