i tried $route.reload(); location.reload(true); $window.location.reload(true);but pending requests are not cancelling/aborting, page is reloading, how to hard reload page means closing all $http requests which are pending and reloading page.
Asked
Active
Viewed 2,406 times
1
-
Have a look at abort() method: http://stackoverflow.com/a/446626/2674883 – Nicolae Olariu Jan 07 '14 at 09:47
-
yeah, like abort i need one global function for $http, so that i can call anywhere? – Manjunath Jan 07 '14 at 10:11
-
http://stackoverflow.com/questions/13928057/how-do-to-cancel-an-http-request-in-angularjs – Nicolae Olariu Jan 07 '14 at 10:44
1 Answers
3
This should do the trick for you. You'll need to call the abortAllPendingRequests
method when you want to cancel your requests.
Please keep in mind that this solution does require the use of underscore. I haven't tried it with lodash.
(function ()
{
angular.module('myApp')
.run(function ($http, $q)
{
$http.currentlyPendingRequests = [];
$http.accessPendingRequests = function (pending)
{
if (!arguments.length) return this.currentlyPendingRequests;
this.currentlyPendingRequests = pending;
};
$http.removeRequest = function (requestUrl)
{
this.currentlyPendingRequests = _.filter(this.currentlyPendingRequests, function (request)
{
return request.url !== requestUrl;
}, this)
};
$http.abortAllPendingRequests = function ()
{
_.each(this.currentlyPendingRequests, function (request)
{
request.aborter.resolve();
});
this.currentlyPendingRequests = [];
};
var originalGet = $http.get;
$http.get = function ()
{
// Ignore template requests
if (arguments[0].indexOf('.html') != -1)
{
return originalGet.apply(this, arguments)
};
var aborter = $q.defer();
var url = arguments[0];
this.currentlyPendingRequests.push({url : url, aborter : aborter});
console.log('pushing url : ' + url);
// Include the abortion promise in the new arguments
var newArgs = [arguments[0], _.extend({}, arguments[1], {timeout : aborter.promise})];
var requestPromise = originalGet.apply(this, newArgs);
// Finally is a reserved word and is not es3 compatible, and therefore non compliant for ie8. Thus the hash
// syntax must be used.
requestPromise['finally'](function()
{
this.removeRequest(url);
});
return requestPromise;
}
});
})();

arete
- 1,903
- 4
- 17
- 23