Setting the cancellable flag to true in the options object when you create your resource allows pending requests to be cancelled. Something like this should work (untested!):
var Resource = $resource('/myresource', null, null, { cancellable: true });
var pending = [];
function newRequest() {
// Cancel previous request
if (pending) pending.$cancelRequest();
// Make a new request
var result = Resource.get();
result.$promise.then(function(resource) {
pending = undefined; // Reset pending when a request was successful
return resource;
}
// Remember this for cancelling
pending = result;
}
$scope.buttonAClicked = function() {
newRequest();
// Whatever else you need to do here
}
$scope.buttonBClicked = function() {
newRequest();
}
You just have to make sure that whenever you make a request, it is saved in pending. If this can happen from more than one place, you may have to use an array.