Reject Angularjs resource promises
Asked Answered
S

0

3

So I'm working on a Angularjs based mobile app. When a user taps on each of the tabs along the bottom a new route is called which is loading the partial and controller.

In my controllers I often then request data from the server via Angularjs $resource such as

$rootScope.getPage = pageApi.query();    // GET
$rootScope.getPage = $rootScope.getPage.$promise.then(function(data) {

  //Do stuff with the returned data

});

I'm using root scope here because I have a spinning loader located outside of the controllers own $scope. While waiting for the response I'm using an Angular directive (Angular Busy) to display a loader.

However as the user moves through the pages on the app, more and more promises are started (that, for example, might not be resolved as the connection has been lost).

How can I resolve / reject any other promises so that only the most recently loaded controller is waiting on a promise ?

All comments / answers are really appreciated!

Thanks, Mac

Solve answered 22/6, 2014 at 14:28 Comment(5)
Why not use current controller's $scope and just update a variable on the $rootScope inside the then callback.Brunson
Check this out github.com/domenic/lastStomachache
I'm having the same issue (#24440677) and can't find a solution anywhere. You'd think this was a common problem. Have you found an answer?Alida
@mac Did you get solution for this issue ?Doodlebug
@MuhammedAthimannil Hey! I believe in Angular JS 1.5 $resource supports a cancellable:true option on 'actions'. You can then call $cancelRequest() on it. With Angular versions below that you can use the timeout option on $httpSolve

© 2022 - 2024 — McMap. All rights reserved.