2014-09-03 18 views
0

我試圖如本的jsfiddle在我的角度應用的app.config部分使用角緩存 - http://jsfiddle.net/0b1jgwoj/在App.config()採用了棱角分明緩存

.config(function (componentFactoryProvider, Config, RestangularProvider, DSCacheFactory, $q) { 
componentFactoryProvider.setViewPath(function (componentSnakeName, componentName) { 
    return 'components/' + componentSnakeName + '/' + componentSnakeName + '.html'; 
}) 
RestangularProvider.setBaseUrl(Config.API.path); 
RestangularProvider.setRequestSuffix('.json'); 

var appCache = DSCacheFactory('appCache', { 
    maxAge: 3600000, 
    deleteOnExpire: 'aggressive', 
    storageMode: 'localStorage', // This cache will sync itself with `localStorage`. 
    onExpire: function (key, value) { 
     //Todo: implement logic to get data from server be it a collection or single object 
    } 
}); 

//Intercept the Get Request and check for value in cache. If found cancel Get Request, if not continue get Request. 
RestangularProvider.addFullRequestInterceptor(function (element, operation, what, url, headers, params, httpConfig) { 
     if(operation === 'get') { 
      debugger; 
      //Check the cache to see if the resource is already cached 
      var data = appCache.get(url); 
      //If cache object does exist, return it 
      if (data !== undefined) { 
       angular.extend(element, data); 

       var defer = $q.defer(); 
       httpConfig.timeOut = defer.promise; 
       defer.resolve(); 

       return { 
        headers: headers, 
        params: params, 
        element: element, 
        httpConfig: httpConfig 
       }; 
      } 
     } 
}); 

RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response) { 
     //Cache the response from a get method 
     if(operation === 'get') { 
      debugger; 
      appCache.remove(url); 
      appCache.put(url, data); 
     } 

     //Unvalidate the cache when a 'put', 'post' and 'delete' is performed to update the cached version. 
     if (operation === 'put' || operation === 'post' || operation === 'delete') { 
      appCache.destroy(); 
     } 

     return response; 
    }); 

})

出現

兩個錯誤(1),即使我已經把它的DI列表裏面$ q沒有定義(2)DSCacheFactory是返回一個未知的提供程序錯誤

就如何解決這些問題的任何想法,這是非常重要的此邏輯保留在.config()部分中,因爲「 addFullRequestInterceptor'不會取消任何其他服務中的請求,但僅取消配置部分中的請求。

謝謝

回答