I have a method that calls an angular service and consequently makes an ajax request via the service. I need to make sure that if this is called several times, the previous request in aborted (if it hasn't already been resolved that is).
This method can get called multiple times. This method is actually from ngTable on ngTableParams
:
getData = function($defer, params) {
myService.getRecord(params).then(function(res){
...
$defer.resolve(res.Records);
});
}
Here's the method on the service:
this.getRecords = function(params) {
...
return Restangular
.all('/api/records')
.post(filters);
};
If ngTable makes 3 calls I want the first 2 to be aborted (unless of course they returned so fast that it got resolved)
This is another approach if you want to abort all http requests when change the state for UI-router:
You can abort
$http
calls via thetimeout
config property, which can be a Promise, that aborts the request when resolved.So in restangular, you can do this like
To integrate it with your usecase, for example, you could have this in your service:
This way calling
getRecords
always aborts the previous call if has not been resolved yet!Hope this helps!