Created
April 30, 2014 20:00
-
-
Save ajhekman/80bd46242aa01ffe794e to your computer and use it in GitHub Desktop.
Loading Status for Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module("LoadingStatus", []) | |
| .factory "Loading", ($q)-> | |
| class Loading | |
| constructor: ()-> | |
| loadingDecorator : (key, context, httpRequestTarget) => | |
| ### | |
| Use this to Decorate (wrap) the call to a $http Promise, and in turn returns a new function to call the $http promise with . | |
| key: string, the key where you want to store the loading state for this | |
| context: object, the "this" where the http request lives | |
| httpRequestTarget: the method call such that | |
| httpRequestTarget.apply(context, httpArgs) calls the $http method | |
| Example: | |
| getUser = loadingDecorator("user", UserService, "getUserFromAPI") | |
| user = getUser({id:123}) | |
| ### | |
| return (httpArgs...) => | |
| @trackLoading( key, httpRequestTarget.apply(context, httpArgs) ) | |
| trackLoading : (key, httpRequest) => | |
| ### | |
| Use this method if you do not want to create a decorator, but still want to track the loading state of a Promise | |
| Example: | |
| user = trackLoading("user", UserService.getUserFromAPI({id:123})) | |
| ### | |
| @[key] = true | |
| stopLoad = (data) => | |
| @[key] = false | |
| return data | |
| stopLoadError = (data) => | |
| @[key] = false | |
| $q.reject data | |
| httpRequest.then stopLoad, stopLoadError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment