2

Hi ive a menu where each button in the menu makes a resource(REST) call. Ive a case where the user clicks button A and then fast clicks button B. In this case I only want to handle the last call and ignore all other made before.

How can I achieve this?

2
  • But what if the call has already reached the server after clicking on button A? How can you possible ignore that? What you could do is to ignore the click on button B in this case, but not on A because it is already sent. Commented May 8, 2016 at 12:42
  • Lets say user clicks on Button A and then fast clicks on Buttton B. In this case the code renders the output from button A and not Button B. This is the issue ive... i want to display the result from the last click. Commented May 8, 2016 at 16:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.