Last active
August 29, 2015 14:02
-
-
Save evan-007/38c70da8a3dd6b856940 to your computer and use it in GitHub Desktop.
f-f-ff-factory
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
//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