【问题标题】:AngularJS Resolve some factory promises for all routesAngularJS 为所有路由解决一些工厂承诺
【发布时间】:2017-03-23 04:35:48
【问题描述】:

我的应用有问题。我有一个工厂,它调用prepareStorage 将我的restApi 中的所有静态变量保存到浏览器存储中(使用ngStorage)。好像是这样的:

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()
         };
}]);

然后我声明了第二个工厂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 对象, 只有当我第二次刷新时才会这样做。

我正在寻找一种避免使用$timeout 服务的干净解决方案。

提前致谢。

【问题讨论】:

    标签: angularjs promise factory ng-storage


    【解决方案1】:

    您是否尝试过使用这种不同的模式?

    准备存储

    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
         }
    
    }]);
    

    我会将 apiService 重构为 Promise 本身,就像

    . . .
    
    //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);
       });
    }
    

    我不能保证这是 100% 正确的,但如果您了解如何使用它,它应该可以工作。在做某事之前,您只需等待承诺返回。我认为你应该像我一样构建你的apiService,这样你也可以在它们上使用.then()

    希望对你有帮助。

    【讨论】:

    • 您好,感谢您的帮助。我试过了 - 首先解决 fex 错误(例如 getStorage -> vm.getStorage),最后在控制器内部调用后返回的对象 getStorage 未定义:constantsService.getStorage() 但响应也是正确的
    • 您好,您能解释一下吗?我不明白你的意思 首先解决 fex 错误(例如 getStorage -> vm.getStorage),最后返回的对象 getStorage 在控制器内部调用后未定义:constantsService.getStorage() 但响应也是正确的
    • PS:apiService方法“call”的初始化如下:codecall: function (method, endpoint, payload, success, error, url) { }
    • 是的,对不起,我在上一条评论 fex 中有错字-> 首先您的脚本代码中有两个错误:1. return { load: vm:load 2. return { getStorage: vm:getStorage }
    • 编辑了我的答案。
    猜你喜欢
    • 2017-01-30
    • 2013-08-09
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多