Skip to content

Instantly share code, notes, and snippets.

@soberstadt
Created September 23, 2015 20:51
Show Gist options
  • Save soberstadt/0abae48a4b1e9b374e88 to your computer and use it in GitHub Desktop.
Save soberstadt/0abae48a4b1e9b374e88 to your computer and use it in GitHub Desktop.
angular.module('missionhub')
.factory('personCache', function() {
// set up variables and constants
var cachedPeople = {};
// define methods
// if you give person() a person object, it will cache it.
// if you give it an id, it will return a person object if it has it.
function person(newValue) {
if (newValue.id) {
cachedPeople[newValue.id] = cachedPeople[newValue.id] || {}
angular.extend(cachedPeople[newValue.id], newValue);
return true;
}
return cachedPeople[newValue];
}
// return interface
return {
person: person
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment