diff --git a/index.html b/index.html new file mode 100644 index 0000000..0110e3a --- /dev/null +++ b/index.html @@ -0,0 +1,414 @@ + + +
+ +ngPromiseStatus is a collection (starting with statusButton) of promise-aware Angular directives that make it easy to bind UI elements to the progress and eventual outcome of a promise.
+ + +This is a simplest use of the directive with only a promise (or array of) passed (and some default styles).
+You can pass custom classes to be applied for each stage of the promise (progress, succcess and error). It plays nicely with Bootstrap btn classes
+The directive exposes several scope properties to the button's content (see docs). You can use them to give extra info on the promise's status.
+Resolved with value 'Done!'
+Rejected with error 'Error!'
+Rejected with error 'error!'
+You can install through bower:
+bower install ng-promise-status+
or through npm:
+npm install ng-promise-status+
or download the latest version from GitHub
+ + +To get started include ngPromiseStatus as a dependency in your angular app:
+var app = angular.module('myApp', ['ngPromiseStatus']);+
and you're good to go!
+ + +The statusButton
directive can be used by simply using the directive as a tag or attribute and passing a promise (or array of).
When the button is clicked an internal handler is fired and assigns a promise handler to the assigned promise. Note that for that reason + new promises are only detected on click.
+Any ng-click
handler assigned to the element will be called before the internal handler so you can easily use the same 'click'
event to both assign a promise in your controller and for it to be detected by the button.
Here's an example:
+Result:
+Template:
++<status-button + ng-click="setSuccessPromise()" + promise="successPromise"> + <span>Success</span> +</status-button> ++
Controller:
++// Create a new promise that will succeed in 3 seconds +$scope.setSuccessPromise = function(){ + // Set $scope.promise with a new promise + $scope.successPromise = $q.defer(); + + // React to promise in controller + $scope.successPromise + .promise + .then(function(){ + console.log('Done from controller'); + }); + + // Set a timeout for 3 seconds and resolve + $timeout(function(){ + $scope.successPromise.resolve('Done!'); + }, 3000); +}; ++
The directive is built to work with multiple type of inputs:
+promise
from a $q.defer()
.deffered
itself, saves the need of saving multiple references on scope.$q.when
).By default, the directive applies 4 classes to the button depending on the state of the promise:
+idle
inprogress
inprogress
is used instead of progress
so that it doesn't clash with Bootstrap progress bars.success
error
ngPromiseStatus intentionally doesn't rely on any additional CSS files or required styling. It provides an easy interface in the form of class management that allows you to easy interact with promises using your existing styles and with no additional bloat to your project.
+ +You can configure a few key features of statusButton - classes applied on promise stages, delay and disable:
+name | +type | +default | +details | +
---|---|---|---|
idle_class |
+ string | +idle |
+ The initial class applied to the button and will applied after completion if a delay is specified. | +
progress_class |
+ string | +inprogress |
+ The class that will be applied to the button whenever a promise is in progress. | +
success_class |
+ string | +success |
+ The class that will be applied to the button when a promise is resolved (completed successfully). | +
error_class |
+ string | +error |
+ The class that will be applied to the button when a promise is rejected (resulted in an error). | +
progress_disable |
+ boolean | +true |
+ A toggle that determines if the button will be disabled when a promise is in progress. | +
delay |
+ number | +0 |
+ The time (in milliseconds) it takes for the button to return to idle state upon promise completion (either success or error). 0 will not return to idle. | +
Here's an example for custom configuration using Bootstrap classes and a 2s delay:
+Result:
+Template:
++<status-button + ng-click="setButtonPromise()" + promise="buttonPromise"> + options="buttonPromise"> + <span>Success</span> +</status-button> ++
Controller:
++// Define your custom configuration +$scope.buttonConfig = { + success_class: 'btn-success', + error_class: 'btn-danger', + progress_class: 'btn-warning', + idle_class: 'btn-default', + delay: 2000 +}; + +// Create a new promise that will succeed in 3 seconds +$scope.setButtonPromise = function(){ + // Set $scope.promise with a new promise + $scope.buttonPromise = $q.defer(); + + // React to promise in controller + $scope.buttonPromise + .promise + .then(function(){ + console.log('Done from controller'); + }); + + // Set a timeout for 3 seconds and reject + $timeout(function(){ + $scope.buttonPromise.reject(); + }, 3000); +}; ++
The directive exposes special scope properties to the button's content that allow you to customize the button's content and interact with the promise's values and state.
+name | +type | +details | +
---|---|---|
$status |
+ string | +The status of the promise, same as the default class names: idle , inprogress , success and error |
+
$value |
+ any | +Whatever value is passed upon resolution or rejection of the promise. If an array of promises is used, $value will be an ordered array of said whatevers. |
+
$done |
+ boolean | +A convenience boolean that indicates if a promise is in progress. | +
$class |
+ string | +The class that is currently applied, overwritten with your custom configuration if provided. | +
Here's an example of using scope properties inside the button (promise is rejected with 'Error!'):
+Result:
+Template:
++<status-button + ng-click="setButtonPromise()" + promise="buttonPromise" + options="bootstrapConfig"> + <span>Success {{$value}}</span> +</status-button> ++