angular-promise-tracker
Version: 2.0
(note to users using version 1.x: upgrading has many breaking changes, see the CHANGELOG.)
Small, feature filled library used to easily add spinners or general promise/request tracking to your angular app.
Quick Start
The basic idea: each time we add one or more promises to an instance of a promiseTracker
, that instance's active()
method will return true until all added promises are resolved. A common use case is showing some sort of loading spinner while some http requests are loading.
Play with this example on plunkr
$ bower install angular-promise-tracker
Loading... Delay Something Fetch Something <!-- optional for $http sugar -->
angular;
API Documentation
promiseTracker
Service -
tracker
promiseTracker([options])Creates and returns a new promiseTracker.
Options can be given as an object, with the following allowed values:
activationDelay
{Number}
- Number of milliseconds that an added promise needs to be pending before this tracker is active.- Usage example: You have some http calls that sometimes return too quickly for a loading spinner to look good. You only want to show the tracker if a promise is pending for over 500ms. You put
{activationDelay: 500}
in options.
- Usage example: You have some http calls that sometimes return too quickly for a loading spinner to look good. You only want to show the tracker if a promise is pending for over 500ms. You put
minDuration
{Number}
- Minimum number of milliseconds that a tracker will stay active.- Usage example: You want a loading spinner to always show up for at least 750ms. You put
{minDuration: 750}
in options.
- Usage example: You want a loading spinner to always show up for at least 750ms. You put
Often you want a global promiseTracker (eg to show a loading screen); one easy way is to put the tracker on your $rootScope:
app;
Instantiated promiseTracker
Example: var myTracker = promiseTracker({ activationDelay: 500, minDuration: 750 });
-
boolean
tracker.active()Returns whether this tracker is currently active. That is, whether any of the promises added to/created by this tracker are still pending. Note: if the
activationDelay
has not elapsed yet, this will return false. -
boolean
tracker.tracking()Returns whether this tracker is currently tracking a request. That is, whether any of the promises added to/created by this tracker are still pending. This method has no regard for
activationDelay
. -
number
tracker.trackingCount()The count of promises currently being tracked.
-
promise
tracker.addPromise(promise)Add any arbitrary promise to tracker.
tracker.active()
will be true untilpromise
is resolved or rejected.promise
{object}
- Promise to add
Usage Example:
var promise = ;myTracker;console; // => true//1000 milliseconds later...console; // => false -
promise
tracker.createPromise()Creates and returns a new deferred object that is tracked by our promiseTracker.
Usage Example:
var deferred = myTrackerconsole; // => truedeferred;console; // => false -
void
tracker.cancel()Causes a tracker to immediately become inactive and stop tracking all current promises.
$http
Sugar
Requires promise-tracker-http-interceptor.js
- Any $http call's
config
parameter can have atracker
field. Examples:
//Add $http promise to tracker with id 'myTracker'
//Add $http promise to both 'tracker1' and 'tracker2'$http
More Examples
- Do something whenever the tracker's active state changes
angular ;
Development
- Install karma & grunt with
npm install -g karma grunt-cli
to build & test - Install local dependencies with
bower install && npm install
- Run
grunt
to lint, test, build the code, and build the docs site - Run
grunt dev
to watch and re-test on changes
New Versions
License
angular-promise-tracker by Andy Joslin is free of known copyright restrictions.