2014-01-08 22 views
0

我有以下提供程序導致應用程序無法執行任何操作,但在控制檯上沒有錯誤...我已經查看了幾個小時無濟於事。角度縮小 - 導致應用程序停止

(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)); 
+0

這可能沒有關係,但爲什麼'this.angular''而不是'angular'作爲變量被傳入閉包? – dcodesmith

+0

@dcodesmith沒有區別。 – Sam

回答

1

我用http://jscompress.com/來縮小和我的應用程序的其餘部分工作正常。無論如何,我沒有測試過你的代碼。也許嘗試下面的縮小代碼或分享你的縮小的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)