Skip to content

Instantly share code, notes, and snippets.

@wesleycho
Created November 30, 2013 04:06
Show Gist options
  • Save wesleycho/7715229 to your computer and use it in GitHub Desktop.
Save wesleycho/7715229 to your computer and use it in GitHub Desktop.
app.factory('api', ['$http', '$q', '$log', function($http, $q, $log) {
return {
last: function(market, currency){
$log.log('api/last', currency, 'market', market);
//return the promise directly.
return $http.get('/api/last/'+market+'/'+currency)
.then(function(result) {
//resolve the promise as the data
$log.log('api.last data', result.data);
return result.data;
});
}
, exchanges: function(){
//return the promise directly.
var deferred = $q.defer();
$http.get('/api/exchanges')
.success(function (data) {
deferred.resolve(data);
});
return deferred.promise;
}
};
}]);
// in OrderCtrl
exchanges.then(function (data) {
$scope.exchanges = data;
});
// or...
resolve: {
exchanges: function ($q, api) {
var deferred = $q.defer();
api.exchange().then(function (data) {
deferred.resolve(data.data.exchanges);
});
return deferred.promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment