fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-06-17 08:03 am

[dw-free] "Can't call inprogress on undefined" error when you click elsewhere while any aj

[commit: http://hg.dwscoalition.org/dw-free/rev/e3d81dc5d30e]

http://bugs.dwscoalition.org/show_bug.cgi?id=3744

Decouple by triggering a custom event instead of digging for the variable
directly (it may not actually exist at that point, which is what's causing
the errors). Also fix a couple issues where the ajaxtip widgets would linger
even when they shouldn't.

Patch by [personal profile] fu.

Files modified:
  • htdocs/js/jquery.ajaxtip.js
  • htdocs/js/jquery.contextualhover.js
--------------------------------------------------------------------------------
diff -r efbcd47a98c8 -r e3d81dc5d30e htdocs/js/jquery.ajaxtip.js
--- a/htdocs/js/jquery.ajaxtip.js	Fri Jun 17 15:54:56 2011 +0800
+++ b/htdocs/js/jquery.ajaxtip.js	Fri Jun 17 16:03:18 2011 +0800
@@ -32,9 +32,22 @@
                 relative: true,
                 effect: "fade",
                 onBeforeShow: function(e) {
-                    var tip = this.getTip();
+                    var tooltipAPI = this;
+                    var tip = tooltipAPI.getTip();
                     tip.removeClass("ajaxresult ajaxresult-success ajaxresult-error")
                         .appendTo("body");
+                    if ( ! tip.data( "boundclose" ) ) {
+                        tip.bind( "close", function () {
+
+                            // abort any existing request
+                            var xhr = tip.data( "xhr" );
+                            if ( xhr ) xhr.abort();
+
+                            // hide any currently shown ones
+                            tooltipAPI.hide();
+                        } );
+                        tip.data( "boundclose", true );
+                    }
 
                     if ( self.options.content && ! this.inprogress ){
                         tip.html(self.options.content)
@@ -84,12 +97,12 @@
         if( tip ) {
             if( tip.inprogress ) return;
             if( tip.isShown() ) tip.hide();
+            tip.inprogress = true;
         }
 
-        tip.inprogress = true;
         self.element.trigger("ajaxstart" + self._namespace());
 
-        $.ajax({
+        var xhr = $.ajax({
             type: opts.formmethod || "POST",
             url : opts.endpoint ? self._endpointurl( opts.endpoint) : opts.url,
             data: opts.data,
@@ -99,15 +112,22 @@
                 var tip = self.element.data("tooltip");
                 if ( tip ) {
                     tip.inprogress = false;
-                    self._reposition( tip.getTip() );
+                    var tipele = tip.getTip();
+                    self._reposition( tipele );
+                    tipele.removeData("xhr");
                 }
             },
             success: opts.success,
             error: opts.error ? opts.error : function( jqxhr, status, error ) {
-                self.element.ajaxtip( "error", "Error contacting server. " + error);
+                if ( status == "abort" )
+                    self.element.ajaxtip("cancel");
+                else
+                    self.element.ajaxtip( "error", "Error contacting server. " + error);
                 self._trigger( "complete" );
             }
         });
+
+        if ( tip ) tip.getTip().data( "xhr", xhr );
     },
     success: function(msg) {
         this.element.trigger({ type: "ajaxresult"+this._namespace(),
@@ -128,8 +148,7 @@
     closeall: function() {
         $(".ajaxtip:visible").each(
             function(){
-                var tip = $(this).prev().data("tooltip");
-                if ( !tip.inprogress ) tip.hide()
+                $(this).trigger("close");
             })
     }
 })
diff -r efbcd47a98c8 -r e3d81dc5d30e htdocs/js/jquery.contextualhover.js
--- a/htdocs/js/jquery.contextualhover.js	Fri Jun 17 15:54:56 2011 +0800
+++ b/htdocs/js/jquery.contextualhover.js	Fri Jun 17 16:03:18 2011 +0800
@@ -349,6 +349,7 @@
                         self.cachedResults[this]=data[this];
                     });
                 }
+                $link.ajaxtip( "cancel" );
                 self._renderPopup();
             }
             self._trigger("complete");
--------------------------------------------------------------------------------