Skip to content

Instantly share code, notes, and snippets.

@ajhekman
Created April 30, 2014 20:00
Show Gist options
  • Select an option

  • Save ajhekman/80bd46242aa01ffe794e to your computer and use it in GitHub Desktop.

Select an option

Save ajhekman/80bd46242aa01ffe794e to your computer and use it in GitHub Desktop.
Loading Status for Angular
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