Skip to content

Instantly share code, notes, and snippets.

@toddbranch
Last active September 28, 2015 16:47
Show Gist options
  • Select an option

  • Save toddbranch/0e87ae3e7eded2bcd38b to your computer and use it in GitHub Desktop.

Select an option

Save toddbranch/0e87ae3e7eded2bcd38b to your computer and use it in GitHub Desktop.
Angular Caching
angular.module('commonContactsModule').factory('contactsService', [
'$q',
'$http',
function($q, $http) {
function getCommon(targetEmail) {
var url = 'https://www.conspire.com/commonContacts?email=' + encodeURIComponent(targetEmail);
var promise;
// want the cache to be global so it can be shared amongst all widgets
if (typeof commonContactsCache === 'undefined') {
commonContactsCache = {};
}
function success(response) {
return response.data;
}
function failure(response) {
return $q.reject(response.status);
}
// if this url is not cached, create a request and add it
if (!commonContactsCache[url]) {
commonContactsCache[url] = $http.get(url).then(success, failure);;
}
// return the cached result
return commonContactsCache[url];
}
return {
getCommon: getCommon
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment