|
1 | 1 | # angular-busy [](https://travis-ci.org/cgross/angular-busy)
|
2 | 2 |
|
3 |
| -> Show busy/loading indicators on any element during $http requests (or any promise). |
4 |
| -
|
5 |
| -This library depends on v1.5 of [Andy Joslin's angular-promise-tracker](https://github.com/ajoslin/angular-promise-tracker). |
6 |
| - |
7 |
| -Annotate an `$http` request using `angular-promise-tracker` and add `cg-busy` on an element to display a busy |
8 |
| -indication on a specific element during the `$http` request. |
9 |
| - |
10 |
| -This library builds on Angular 1.2 and the new Angular animate module in `animate.js`. |
11 |
| - |
12 |
| -Supports IE 10, and recent versions of FF and Chrome. |
| 3 | +> Show busy/loading indicators on any $http or $resource request, or on any promise. |
13 | 4 |
|
14 | 5 | ## Demo
|
15 | 6 |
|
16 | 7 | [Live Demo](http://cgross.github.io/angular-busy/demo)
|
17 | 8 |
|
18 | 9 | ## Getting Started
|
19 | 10 |
|
20 |
| -Add `dist/angular-busy.js` and `dist/angular-busy.css` to your index.html. Also add the `angular-promise-tracker` files as necessary. |
| 11 | +Install with Bower or download the the files directly from the dist folder in the repo. |
| 12 | + |
| 13 | +```bash |
| 14 | +bower install angular-busy --save |
| 15 | +``` |
| 16 | + |
| 17 | +Add `dist/angular-busy.js` and `dist/angular-busy.css` to your index.html. |
21 | 18 |
|
22 |
| -Add `cgBusy` as a module dependency for your module (in addition to `ajoslin.promise-tracker` and the Angular 1.2 `ngAnimate` module): |
| 19 | +Add `cgBusy` as a module dependency for your module. |
23 | 20 |
|
24 | 21 | ```js
|
25 |
| -angular.module('your_app', ['ngAnimate','ajoslin.promise-tracker','cgBusy']); |
| 22 | +angular.module('your_app', ['cgBusy']); |
26 | 23 | ```
|
27 | 24 |
|
28 |
| -Add the promise trackers as you normally would using `angular-promise-tracker`: |
| 25 | +Add your promise to $scope and reference that in the `cg-busy` directive: |
29 | 26 |
|
30 | 27 | ```js
|
31 |
| -function MyCtrl($scope) { |
| 28 | +function MyCtrl($scope,$http,User) { |
32 | 29 |
|
33 |
| - $scope.pizzaFlavor = $http.get('/pizzaFlavor', { tracker: 'pizza' }); |
| 30 | + //using $http |
| 31 | + $scope.myPromise = $http.get('...'); |
| 32 | + |
| 33 | + //if you have a User class based on $resource |
| 34 | + $scope.myPromise = User.$save(); |
34 | 35 |
|
35 | 36 | }
|
36 | 37 | ```
|
37 | 38 |
|
38 |
| -Add `cg-busy` to the elements you wish to be _busy_ during those requests: |
39 |
| - |
40 | 39 | ```html
|
41 |
| -<div cg-busy="'pizza'"></div> |
| 40 | +<!-- Use the simple syntax --> |
| 41 | +<div cg-busy="myPromise"></div> |
| 42 | + |
| 43 | +<!-- Use the advanced syntax --> |
| 44 | +<div cg-busy="{promise:myPromise,message:'Loading Your Data',templateUrl:'mycustomtemplate.html'}"></div> |
42 | 45 | ```
|
43 | 46 |
|
44 | 47 | ## Options
|
45 | 48 |
|
46 |
| -The `cg-busy` directive expects a value that is interpreted as an expression. The value may be specified as an object literal |
47 |
| - or simply as a string if only the `tracker` value is provided. |
| 49 | +The `cg-busy` directive expects either a promise or a configuration object. |
48 | 50 |
|
49 | 51 | In other words. You may do this:
|
50 | 52 |
|
51 | 53 | ```html
|
52 |
| -<div cg-busy="'my_tracker'"></div> <!-- Notice the extra single quotes because its an expression --> |
| 54 | +<div cg-busy="myPromise"></div> |
53 | 55 | ```
|
54 | 56 |
|
55 | 57 | or this:
|
56 | 58 |
|
57 | 59 | ```html
|
58 |
| -<div cg-busy="{tracker:'my_tracker',backdrop:false,template:'myAwesomeTemplate.html'}"></div> |
| 60 | +<div cg-busy="{promise:myPromise,message:'Loading',backdrop:false,templateUrl:'myAwesomeTemplate.html',delay:300,minDuration:700}"></div> |
59 | 61 | ```
|
60 | 62 |
|
61 |
| -* `tracker` - Required. The name(s) of the promise tracker. May either be a string or an array of strings if you wish to use the same indicator for multiple promises/trackers. |
| 63 | +* `promise` - Required. The promise (or array of promises) that will cause the busy indicator to show. |
| 64 | +* `message` - Optional. Defaults to 'Please Wait...'. The message to show in the indicator. This value may be updated while the promise is active. The indicator will reflect the updated values as they're changed. |
62 | 65 | * `backdrop` - Optional. Boolean, default is true. If true a faded backdrop will be shown behind the progress indicator.
|
63 |
| -* `template` - Optional. If provided, the given template will be shown in place of the default progress |
64 |
| -indicatory template. Use this to override the default UI and provide your own. |
| 66 | +* `templateUrl` - Optional. If provided, the given template will be shown in place of the default progress indicatory template. |
| 67 | +* `delay` - Optional. The amount of time to wait until showing the indicator. Defaults to 0. Specified in milliseconds. |
| 68 | +* `minDuration` - Optional. The amount of time to keep the indicator showing even if the promise was resolved quicker. Defaults to 0. Specified in milliseconds. |
65 | 69 |
|
66 | 70 | ## Providing Custom Templates
|
67 | 71 |
|
68 |
| -The default progress template shows a spinner and a 'Please Wait...' message. But you can define custom templates per instance |
69 |
| -(as shown above) or change the global default template. To change the global default template just provide a new |
70 |
| -`$injector` value for `cgBusyTemplateName`. Ex: |
| 72 | +The angular-busy indicator is a regular Angular template. The templates have access to the scope where `cg-busy` was declared so you may reference your local scope variables in your custom templates. Additionally, the scope is augmented with a `$message` field containing the indicator message text. |
| 73 | + |
| 74 | +## Overriding Defaults |
71 | 75 |
|
72 |
| - ```js |
73 |
| -angular.module('yourapp').value('cgBusyTemplateName','your_custom_template_here.html'); |
| 76 | +The defaut values for `message`, `backdrop`, `templateUrl`, `delay`, and `minDuration` may all be overriden by overriding the `$injector` value for `cgBusyDefaults`, like so: |
| 77 | + |
| 78 | +```js |
| 79 | +angular.module('your_app').value('cgBusyDefaults',{ |
| 80 | + message:'Loading Stuff', |
| 81 | + backdrop: false, |
| 82 | + templateUrl: 'my_custom_template.html', |
| 83 | + delay: 300, |
| 84 | + minDuration: 700 |
| 85 | +}); |
74 | 86 | ```
|
75 | 87 |
|
76 |
| -Templates are full, normal Angular partials with access to the scope of where the `cg-busy` was used. |
| 88 | +Only the values you'd like overriden need to be specified. |
| 89 | + |
77 | 90 |
|
78 | 91 | ## Release History
|
| 92 | + * v4.0.0 - Big update |
| 93 | + * Dependency on angular-promise-tracker has been removed. We now track promises directly. |
| 94 | + * Message is now configurable. |
| 95 | + * The template options is now templateUrl. |
| 96 | + * The delay option has been added. |
| 97 | + * The minDuration option has been added. |
| 98 | + * Changing default template has been modified to be part of the new `cgBusyDefaults` value. |
79 | 99 | * v3.0.2 - Reverting back to promise-tracker v1.5 due to changes in the api.
|
80 | 100 | * v3.0.1 - Fix for using cg-busy when a tracker has already been registered.
|
81 | 101 | * v3.0.0 - Support for new promise-tracker api. Fix for multiple cg-busy's on the same scope.
|
|
0 commit comments