My previous question highlighted caching possibilities in my services.
The documentation for ngResource (v1.3.0-build.2417 (snapshot) at the time of writing this) shows a cache flag.
However, the cache will only be populated after the first call to service.get(id). I want to be able to pre-populate the resource cache with an item that was retrieved from elsewhere
(It's perfectly reasonable to have the same item be available from 2+ endpoints. E.g. you can have a task available at /task/5 and as part of a collection at /projects/99/tasks if task 5 is part of project 99)
I've tried this, but it is very ugly:
// return the API in the project service
return {
project: null, // my own hand-rolled cache
get: function (id) {
// check cache
if (this.project != null && this.project.id == id) {
console.log("Returning CACHED project", this.project);
var future = $q.defer();
future.resolve(this.project);
// UGLY: for consistent access in the controller
future.$promise = future.promise;
return future;
} else {
return Projects.get({
id: id
});
}
}, // etc
And in the controller:
$q.when(projectService.get($routeParams.id).$promise).then(function(project) {
// etc
How do I pre-populate the cache using AngularJS idioms?