2016-11-09 9 views
1

私のアプリに問題があります。私は工場prepareStorageを呼び出して、restApiからすべての静的な変数を(ngStorageを使用して)ブラウザストレージに保存しています。それはこのように思える:AngularJSすべてのルートについて工場出荷時の約束を解決します

angular.module('panelApp') 
     .factory('prepareStorage', ['$localStorage', '$sessionStorage', 'apiService', 

    function($localStorage, $sessionStorage, apiService) { 
    var promise = {}; 
    var load = function() { 
    apiService.call('head', 'staticTypes.json', [], function(response, status, head) { 

    if(angular.isUndefined($localStorage.staticTypes) || $localStorage.staticTypes.updatedAt < new Date(head()['last-modified'])){ 
     delete $localStorage.staticTypes; 
     promise = apiService.call('get', 'statictypes.json', [], function (response) { 
      $localStorage.staticTypes = response; 
     }, function (errorObj) { 
      console.log(errorObj.error.message); 
     }); 
    } 
    }); 


    return promise; 

    return { 
      load: load() 
     }; 
}]); 

その後、私は私のストレージVARSを与える第二工場constantsService、宣言している:

angular.module('panelApp') 
    .factory('constantsService', ['$localStorage', '$q', 'prepareStorage', '$timeout', 

    function($localStorage, $q, prepareStorage, $timeout) { 
    var output = {}; 
    var deferred = $q.defer(); 

    $timeout(function() { 
     deferred.resolve(prepareStorage.load, function() { 
      output = { 
       status: $localStorage.staticTypes.status 
      }; 
     }); 
    }, 1000); 

    return output; 

}]); 

をそして今、この時の私の悩みが来ます。

の再リフレッシュなしで、このストレージバーをコントローラ内に取得できません。つまり、最初のサイトの更新後に、コントローラ は$localStorage.staticTypes.statusオブジェクトを検出しません。 2回目のリフレッシュを行う場合にのみ実行されます。

私は、$timeoutサービスの使用を避けるためのきれいな解決策を模索しています。

ありがとうございます。

答えて

0

このパターンを試しましたか?

prepareStorage

angular.module('panelApp') 
     .factory('prepareStorage', prepareStorage); 

prepareStorage.$inject = [ '$localStorage', '$sessionStorage', 'apiService' ]; 

    function prepareStorage ($localStorage, $sessionStorage, apiService) { 

    var vm = this; 

    vm.load = function() { 
     return new Promise(function(resolve, reject){ 
      apiService.call('head', 'staticTypes.json', [], function(response, status, head) { 
      if(angular.isUndefined($localStorage.staticTypes) || $localStorage.staticTypes.updatedAt < new Date(head()['last-modified'])){ 
      delete $localStorage.staticTypes; 
      apiService.call('get', 'statictypes.json').then(function (response, error) { 
       if(response){ 
       $localStorage.staticTypes = response; 
       return resolve(response); 
       } 
       return reject(error); 
      }); 
      } 
      }); 
     }); 
    } 

    return { 
      load: load 
     }; 
}); 

そしてconstantsService

angular.module('panelApp') 
    .factory('constantsService', constantsService); 

    constantsService.$inject = [ '$localStorage', '$q', 'prepareStorage', '$timeout' ]; 

    function constantsService($localStorage, $q, prepareStorage, $timeout) { 

    var vm = this; 

    vm.getStorage = function(){ 
    prepareStorage.load().then(function(response){ 
     return response; 
    }); 
    } 


    return : { 
       getStorage : getStorage 
    } 

}]); 

そして私は

. . . 

//E.g. Method = get, document = statictypes.json 
vm.call = function(method, document){ 
    return new Promise(function (resolve, reject){ 
     ... do something 
     if(everyThingIsOk){ 
     return resolve(response); 
     } 

     return reject(error); 
    }); 
} 
よう Promiseそのものであることを apiServiceをリファクタリングう

これは正しい100%保証はできませんが、使用方法を理解していればうまくいくはずです。何かをする前に戻ることを約束するだけです。私は、私がしたようにあなたのapiServiceを構造化すべきだと思うので、.then()も使えます。

希望です。

+0

こんにちは、ご協力いただきありがとうございます。私はそれを試しました - まず最初に、getStorage - > vm.getStorageなどのfexエラーを解決し、最後に返されたオブジェクトgetStorageはコントローラ内の呼び出しの後に定義されていません: constantsService.getStorage()でも応答は正しいです – Joeker

+0

より良い?私はあなたが何を意味するのか理解できませんでした*最初にfxエラーを解決しました(例えばgetStorage - > vm.getStorage)そして最終的に返されるオブジェクトgetStorageはコントローラ内の呼び出しの後で定義されていません:constantsService.getStorage()でも応答は正しい* – AndreaM16

+0

PS:apiServiceメソッド "call"の初期化は次のとおりです。 'code'call:function(メソッド、エンドポイント、ペイロード、成功、エラー、URL){} – Joeker

関連する問題