2015-02-24 34 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(); 
    }) 
相關問題