Skip to content

Instantly share code, notes, and snippets.

@evan-007
Last active August 29, 2015 14:02
Show Gist options
  • Save evan-007/38c70da8a3dd6b856940 to your computer and use it in GitHub Desktop.
Save evan-007/38c70da8a3dd6b856940 to your computer and use it in GitHub Desktop.
f-f-ff-factory
//factory returns entire API response, controller is responsible for parsing the return value
.factory('CapitalData', function(API_AUTH, $http, $q, SEARCH_PATH){
return function(countryId, capital){
var defer = $q.defer();
$http.get(SEARCH_PATH+'&name_equals='+capital+'&country='+countryId+API_AUTH)
.success(function(data){
defer.resolve(data);
});
return defer.promise;
};
});
.controller('countryCtrl', function($scope, CapitalData){
CapitalData(id, capital).then(function(data){
$scope.capital = data.geonames[0]; //controller takes what it needs from the response
});
});
//option 2:
//controller knows nothing about CountryData's return format
.factory('CapitalData', function(API_AUTH, $http, $q, SEARCH_PATH){
return function(countryId, capital){
var defer = $q.defer();
$http.get(SEARCH_PATH+'&name_equals='+capital+'&country='+countryId+API_AUTH)
.success(function(data){
defer.resolve(data.geonames[0]); //factory is responsible for passing correct data
});
return defer.promise;
};
});
.controller('countryCtrl', function($scope, CapitalData){
CapitalData(id, capital).then(function(data){
$scope.capital = data; //controller is spoonfed data from service
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment