2015-02-24 35 views
0

我正在将缓存添加到我的应用程序中,一切正常,缓存数据,使其过期并重新缓存。当我实现的把相同的过期数据,重新进行缓存的功能,如果没有连接我得到这个错误:Cannot read property 'then' of undefinedangularJS/ionic无法读取未定义属性'then'

这里是我的Controler代码:

.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

功能getItems ISN不会返回任何东西 - 它看起来像你想要返回一个承诺,所以它应该是下面的东西。

当您在缓存中找到该项目时,您可以使用$ 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(); 
}) 
+0

我想你的解决方案没有奏效,我也尝试应用概念,指出错误的方式ALS,没有工作仍然infinate HTTP调用和undifined同样的错误生病后我soultion这并无法正常工作一个答案 – 2015-02-24 13:33:29

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(); 
    }) 
相关问题