angular-poller
angular-poller copied to clipboard
promise that is resolved on each call to backend
I am trying to figure out how to get the promise that is resolved each time the poller ticks. I want to show a quick busy indicator starting when the poller goes to the backend and ending when the callback is complete. Is this possible without changing the poller?
You could use an http intercept...you may have to do a bit of path matching on the calls you want this on, but you could then flag/broadcast on request and then on response.
Thanks Justin. After much learning as an Angular newbie, I've gotten this to work.
I wrote an $Http interceptor which broadcasts the $http config object for each request. In my controllers where the pollers are defined, I receive these broadcasts and look for ones that have the same HTTP method and Url. I create a promise and assign it to the controller scope. The interceptor also broadcasts responses and response errors. The controller also receives these and after checking to see if the HTTP method and Url matches, resolves the promise on the scope. I am using the angular-busy module which expects a promise to show a waiting indicator until the promise is resolved. http://cgross.github.io/angular-busy/demo/
Yup, thats sounds good. It is a bit more code than you'd ideally want to do this but does avoid amending the poller.
As a side note, the poller uses a deferred notify callback. I haven't really looked deep enough at the poller code yet to work out why it does, rather than allowing the promise to be used in the usual success, fail, notify pattern.
For instance, the notify could be used for informing of progress, that does away with the above intercept, and then allow the developer to implement their own success and fail if they wish to.