Last active
September 28, 2015 16:47
-
-
Save toddbranch/0e87ae3e7eded2bcd38b to your computer and use it in GitHub Desktop.
Angular Caching
This file contains hidden or 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('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