I am trying to figure out if the $http interceptor can be used to cancel the request before it occurs.
There is a button that launches the request, but if the user double-clicks it, I do not want the same request to be turned on twice.
Now I understand that there are several ways to solve this problem, and we already have a working solution where we wrap $http in a service that tracks requests that are currently pending and simply ignores new requests using the same method URL and data.
This is basically the behavior that I'm trying to do with an interceptor:
factory('httpService', ['$http', function($http) { var pendingCalls = {}; var createKey = function(url, data, method) { return method + url + JSON.stringify(data); }; var send = function(url, data, method) { var key = createKey(url, data, method); if (pendingCalls[key]) { return pendingCalls[key]; } var promise = $http({ method: method, url: url, data: data }); pendingCalls[key] = promise; promise.finally(function() { delete pendingCalls[key]; }); return promise; }; return { post: function(url, data) { return send(url, data, 'POST'); } } }])
When I look at the $http interceptor API, this is not like achieving this. I have access to the config object, but more on that.
Am I trying to go beyond what interceptors can be used here or is there a way to do this?
angularjs
ivarni Feb 28 '14 at 9:13 2014-02-28 09:13
source share