Clean up active xhr

Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/392f7b3e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/392f7b3e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/392f7b3e

Branch: refs/heads/api-options
Commit: 392f7b3ec78bcd6eca2e9661afabbd4ec39c5337
Parents: b9d30b1
Author: Garren Smith <[email protected]>
Authored: Thu Dec 19 15:52:22 2013 +0200
Committer: suelockwood <[email protected]>
Committed: Tue Jan 14 10:45:55 2014 -0500

----------------------------------------------------------------------
 src/fauxton/app/api.js    | 41 ++++++++++++++++++++++++++++++++++++++---
 src/fauxton/app/router.js |  2 +-
 2 files changed, 39 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/392f7b3e/src/fauxton/app/api.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js
index f423ef5..6d450de 100644
--- a/src/fauxton/app/api.js
+++ b/src/fauxton/app/api.js
@@ -290,6 +290,7 @@ function(app, Fauxton) {
     establish: function() {},
     route: function() {},
     roles: [],
+    _promises: [],
     initialize: function() {}
   }, {
 
@@ -319,7 +320,9 @@ function(app, Fauxton) {
       }
 
       triggerBroadcast('beforeEstablish');
-      FauxtonAPI.when(this.establish()).then(function(resp) {
+      var establishPromise = this.establish();
+      this.addPromise(establishPromise);
+      FauxtonAPI.when(establishPromise).then(function(resp) {
         triggerBroadcast('afterEstablish');
         _.each(routeObject.getViews(), function(view, selector) {
           if(view.hasRendered) { 
@@ -328,6 +331,8 @@ function(app, Fauxton) {
           }
 
           triggerBroadcast('beforeRender', view, selector);
+          var viewPromise = view.establish();
+          routeObject.addPromise(viewPromise);
           FauxtonAPI.when(view.establish()).then(function(resp) {
             masterLayout.setView(selector, view);
 
@@ -339,7 +344,7 @@ function(app, Fauxton) {
                 reason: resp
               };
 
-              if (resp) { 
+              if (resp && resp.responseText) { 
                 var errorText = JSON.parse(resp.responseText).reason;
                 FauxtonAPI.addNotification({
                   msg: 'An Error occurred: ' + errorText,
@@ -353,7 +358,7 @@ function(app, Fauxton) {
 
         });
       }.bind(this), function (resp) {
-          if (!resp) { return; }
+          if (!resp || !resp.responseText) { return; }
           FauxtonAPI.addNotification({
                 msg: 'An Error occurred' + 
JSON.parse(resp.responseText).reason,
                 type: 'error',
@@ -422,6 +427,36 @@ function(app, Fauxton) {
       }, this);
     },
 
+    addPromise: function (promise) {
+      if (_.isEmpty(promise)) { return; }
+
+      if (_.isArray(promise)) {
+        return _.each(promise, function (p) {
+          this._promises.push(p);
+        }, this);
+      }
+
+     this._promises.push(promise);
+    },
+
+    cleanup: function () {
+      this.removeViews();
+      this.rejectPromises();
+    },
+
+    rejectPromises: function () {
+      _.each(this._promises, function (promise) {
+        if (promise.state() === "resolved") { return; }
+        if (promise.abort) {
+          return promise.abort("Route change");
+        } 
+
+        promise.reject();
+      }, this);
+
+      this._promises = [];
+    },
+
     getRouteUrls: function () {
       return _.keys(this.get('routes'));
     },

http://git-wip-us.apache.org/repos/asf/couchdb/blob/392f7b3e/src/fauxton/app/router.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js
index 7cf69ad..89c60cf 100644
--- a/src/fauxton/app/router.js
+++ b/src/fauxton/app/router.js
@@ -76,7 +76,7 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, 
LoadAddons) {
           authPromise.then(function () {
             if (!that.activeRouteObject || 
!that.activeRouteObject.hasRoute(route)) {
               if (that.activeRouteObject) {
-                that.activeRouteObject.removeViews();
+                that.activeRouteObject.cleanup();
               }
               that.activeRouteObject = new RouteObject(route, masterLayout, 
args);
             }

Reply via email to