Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active February 9, 2020 07:53
Show Gist options
  • Save mattbrailsford/51fe762741bbb1515261dddf884faff8 to your computer and use it in GitHub Desktop.
Save mattbrailsford/51fe762741bbb1515261dddf884faff8 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
function MyController($scope, vendrMyResource, vendrRouteCache)
{
vendrRouteCache.getOrFetch("currentThing", () => vendrMyResource.getMyThing()).then(function (thing) {
// TODO: Handle my thing
console.log(thing);
});
}
angular.module('vendr').controller('Vendr.Controllers.MyController', MyController);
}());
(function () {
'use strict';
var cache = new Map();
function vendrRouteCache($rootScope, $q) {
var api = {
getOrFetch: function (key, action) {
if (!cache.has(key)) {
cache.set(key, action().then(function (data) {
cache.set(key, data);
return data;
}));
}
return $q.when(cache.get(key));
},
get: function (key) {
return $q.when(cache.has(key) ? cache.get(key) : null);
},
clearAll: function () {
cache.clear();
}
};
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
api.clearAll();
});
return api;
};
angular.module('vendr.services').factory('vendrRouteCache', vendrRouteCache);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment