Created
January 21, 2014 00:47
-
-
Save stevenharman/8532288 to your computer and use it in GitHub Desktop.
Wrapping AngularJS $http calls in a promise, while also being able to massage the returned data, is pretty common. Especially when tucking that $http concerns behind a service. So, I made a `defer` helper that will create a promise for me!
This file contains 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('brewdegaCellar').factory 'ImportMatchReport', ($http, $q) -> | |
defer = (f)-> | |
deferred = $q.defer() | |
f(deferred) | |
return deferred.promise | |
show: ()-> | |
defer (deferred)-> | |
$http.get('/import/match_report') | |
.success((data)-> deferred.resolve(data.matchReport)) | |
.error(-> deferred.reject('An error occurred while fetching the match report.')) | |
confirm: (match)-> | |
defer (deferred)-> | |
$http.post(match.links.confirmation) | |
.success((data)-> deferred.resolve(data.match)) | |
.error(-> deferred.reject("An error occurred while confirming the Match #{match.id}")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment