Last active
February 9, 2020 07:53
-
-
Save mattbrailsford/51fe762741bbb1515261dddf884faff8 to your computer and use it in GitHub Desktop.
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
(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); | |
}()); |
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
(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