0

I have the following provider that is causing the application to not do anything, yet no errors on the console... I have looked at it for hours to no avail.

(function (angular) { "use strict"; angular.module('app').provider('templateRepository', function () { var templateKey = 'template:', useCache = true, cacheDuration = 30, autoCleanCache = true, cleanCacheInterval = 30; return { $get: ['$http', '$q', '$interval', function ($http, $q, $interval) { var getFromCache = function (key) { return JSON.parse(localStorage.getItem(key)); }, getFromServer = function (name) { return $http.get('Template/Index?name=' + name).then(function (response) { return response.data; }); }, isExpired = function (item) { return ((parseInt(new Date() - new Date(item.timestamp)) / (1000 * 60)) >= cacheDuration); }, removeFromCache = function (key) { localStorage.removeItem(key); }, saveToCache = function (key, value) { localStorage.setItem(key, JSON.stringify({ timestamp: new Date().toUTCString(), value: value })); }, cleanCache = function () { for (var key in localStorage) { if (key.substring(0, 9) === templateKey) { if (isExpired(JSON.parse(localStorage.getItem(key)))) { removeFromCache(key); } } } }; if (useCache && autoCleanCache) { $interval(function () { cleanCache(); }, (cleanCacheInterval * 60 * 1000)); } return { get: function (name) { var key = templateKey + name, d = $q.defer(), item; if (useCache) { item = getFromCache(key); if (item === null) { getFromServer(name).then(function (template) { saveToCache(key, template); d.resolve(template); }); } else { if (isExpired(item)) { getFromServer(name).then(function (template) { saveToCache(key, template); d.resolve(template); }); } else { d.resolve(item.value); } } } else { getFromServer(name).then(function (template) { d.resolve(template); }); } return d.promise; } }; }], configureCache: function (enable, duration, autoClean, cleanInterval) { useCache = enable; cacheDuration = duration; autoCleanCache = autoClean; cleanCacheInterval = cleanInterval; } }; }); }(this.angular)); 
2
  • This might not be related but why this.angular and not just angular as the variable being passed into the closure? Commented Jan 8, 2014 at 22:28
  • @dcodesmith Makes no difference. Commented Jan 8, 2014 at 22:31

1 Answer 1

1

I used http://jscompress.com/ to minify and the rest of my application worked fine. I didn't test your code in anyway. Perhaps try the below minified code or share your minified js.

(function(e){"use strict";e.module("app").provider("templateRepository",function(){var e="template:",t=true,n=30,r=true,i=30;return{$get:["$http","$q","$interval",function(s,o,u){var a=function(e){return JSON.parse(localStorage.getItem(e))},f=function(e){return s.get("Template/Index?name="+e).then(function(e){return e.data})},l=function(e){return parseInt(new Date-new Date(e.timestamp))/(1e3*60)>=n},c=function(e){localStorage.removeItem(e)},h=function(e,t){localStorage.setItem(e,JSON.stringify({timestamp:(new Date).toUTCString(),value:t}))},p=function(){for(var t in localStorage){if(t.substring(0,9)===e){if(l(JSON.parse(localStorage.getItem(t)))){c(t)}}}};if(t&&r){u(function(){p()},i*60*1e3)}return{get:function(n){var r=e+n,i=o.defer(),s;if(t){s=a(r);if(s===null){f(n).then(function(e){h(r,e);i.resolve(e)})}else{if(l(s)){f(n).then(function(e){h(r,e);i.resolve(e)})}else{i.resolve(s.value)}}}else{f(n).then(function(e){i.resolve(e)})}return i.promise}}}],configureCache:function(e,s,o,u){t=e;n=s;r=o;i=u}}})})(this.angular) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.