将缓存添加到我的应用程序一切正常,从缓存数据,使其过期并重新缓存。当我实现了将相同的过期数据再次放入缓存中的功能时,如果没有连接,我收到此错误:Cannot read property 'then' of undefined
以下是我的控制器的代码:
.controller('ItemsCtrl', function($scope, $http, DSCacheFactory) {
//handle the catche to hold the items lits.
self.itemsCache = DSCacheFactory.get("itemsCache");
/*
*Fcuntion to re-refresh the cach after expire
*re-use the old cache if no internet connection available
*/
self.itemsCache.setOptions({
onExpire: function(key, value){
getItems()
.then(function(){
console.log("Items Cache was automatically refreshed", new Date());
}, function(){
console.log("Errorgetting data put expired item back in the cache", new Date());
});
}
});
function getItems(){
var cacheKey = "items",
itemsData = self.itemsCache.get(cacheKey);
if(itemsData){
// if data in the cache dont make HTTP calls.
console.log("Found Data in cache", itemsData);
//recive Data from the cache
$scope.items = itemsData;
}
else{//if no data in the catch make HTTP calls.
//HTTP request to get the items data.
$http.get('services/data.json')
.success(function(data){
console.log("Recived data via HTTP");
//caching the data recived from the http calls.
self.itemsCache.put(cacheKey, data);
$scope.items = data;
});
}//end of the else statment
}
getItems();
})
答案 0 :(得分:0)
函数getItems没有返回任何东西 - 看起来你想要返回一个promise,所以它应该是下面的东西。
当您在缓存中找到该项时,您可以使用$ q.when创建一个在参数不是承诺时已经解决的承诺。
当您找不到它时,您可以从$ http.get返回链式承诺。
.controller('ItemsCtrl', function($scope, $http, DSCacheFactory, $q) {
//handle the catche to hold the items lits.
self.itemsCache = DSCacheFactory.get("itemsCache");
/*
*Fcuntion to re-refresh the cach after expire
*re-use the old cache if no internet connection available
*/
self.itemsCache.setOptions({
onExpire: function(key, value){
getItems()
.then(function(itemsData){
console.log("Items Cache was automatically refreshed", new Date());
}, function(){
console.log("Errorgetting data put expired item back in the cache", new Date());
});
}
});
function getItems(){
var cacheKey = "items",
itemsData = self.itemsCache.get(cacheKey);
if(itemsData){
// if data in the cache dont make HTTP calls.
console.log("Found Data in cache", itemsData);
//recive Data from the cache
$scope.items = itemsData;
return $q.when(itemsData);
}
else{//if no data in the catch make HTTP calls.
//HTTP request to get the items data.
return $http.get('services/data.json')
.success(function(data){
console.log("Recived data via HTTP");
//caching the data recived from the http calls.
self.itemsCache.put(cacheKey, data);
$scope.items = data;
return data;
});
}//end of the else statment
}
getItems();
})
答案 1 :(得分:0)
这不起作用我只是不想破坏这个问题。
.controller('ItemsCtrl', function($scope, $http, $q, DSCacheFactory) {
//handle the catche to hold the items lits.
self.itemsCache = DSCacheFactory.get("itemsCache");
/*
*Fcuntion to re-refresh the cach after expire
*re-use the old cache if no internet connection available
*/
self.itemsCache.setOptions({
onExpire: function(key, value){
getItems()
.then(function(){
console.log("Items Cache was automatically refreshed", new Date());
}, function(){
console.log("Errorgetting data put expired item back in the cache", new Date());
});
}
});
function getItems(){
var deferred = $q.defer(),
cacheKey = "items",
itemsData = self.itemsCache.get(cacheKey);
if(itemsData){
// if data in the cache dont make HTTP calls.
console.log("Found Data in cache", itemsData);
//recive Data from the cache
$scope.items = itemsData;
deferred.resolve(itemsData);
}
else{//if no data in the catch make HTTP calls.
//HTTP request to get the items data.
$http.get('services/data.json')
.success(function(data){
console.log("Recived data via HTTP");
//caching the data recived from the http calls.
self.itemsCache.put(cacheKey, data);
$scope.items = data;
deferred.resolve(data);
});
}//end of the else statment
}
getItems();
})