diff options
author | Garren Smith <garren.smith@gmail.com> | 2013-12-19 15:52:22 +0200 |
---|---|---|
committer | suelockwood <deathbear@apache.org> | 2014-01-14 10:45:55 -0500 |
commit | 392f7b3ec78bcd6eca2e9661afabbd4ec39c5337 (patch) | |
tree | ce3f6d5b4e7c78b6da1fac262ec38274bdd7e166 | |
parent | b9d30b1483f731358b8ace6dc052a2641132f16a (diff) | |
download | couchdb-392f7b3ec78bcd6eca2e9661afabbd4ec39c5337.tar.gz |
Clean up active xhr
-rw-r--r-- | src/fauxton/app/api.js | 41 | ||||
-rw-r--r-- | src/fauxton/app/router.js | 2 |
2 files changed, 39 insertions, 4 deletions
diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index f423ef512..6d450deaa 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')); }, diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index 7cf69ade0..89c60cf33 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); } |