service返回promise,但cachingService没有

时间:2015-01-21 21:33:34

标签: javascript angularjs angular-services

我正在调用getWeeklyDates,它正在调用cachingGlobalConfigurationService,如果在localstorage中找不到globalConfiguration数据,它将再次调用globalConfigurationService。

代码=>

return cachingGlobalConfigurationService.getGlobalConfiguration()
                   .then(function(response1){

在没有缓存globalConfiguration时工作正常,因为我进行了ajax调用并返回了一个promise。

但上面的代码行.then(函数(response1)是未定义的,当我的globalConfiguration可以在localStorage中找到时,只返回:

 else {
          return cachedGlobalConfiguration;
        }

我想在这种情况下我不能使用.then,但我想。

我该如何解决?

1

this.getWeeklyDates= function (projectId, currentDate) {

        return cachingGlobalConfigurationService.getGlobalConfiguration()
               .then(function(response1){

        // do business logic
    });

2

'use strict';
angular.module('test').service('cachingGlobalConfigurationService', function (localStorageService, globalConfigurationService) {
  this.getGlobalConfiguration = function () {

    var cachedGlobalConfiguration = localStorageService.get('globalConfiguration');
    if (!cachedGlobalConfiguration) {
      return globalConfigurationService.getGlobalConfiguration().then(
        function (globalConfiguration) {
          localStorageService.set('globalConfiguration', globalConfiguration);
          return globalConfiguration;
        },
        function (error) {
          console.log('error', error);
        });
    }
    else {
      return cachedGlobalConfiguration;
    }
  };

  this.saveGlobalConfiguration = function (globalConfiguration) {

    // TODO: Only save to local storage when service.save was successfully
    localStorageService.set('globalConfiguration', globalConfiguration);
    globalConfigurationService.saveGlobalConfiguration(globalConfiguration);
  }
});

第3

'use strict';
angular.module('test').service('globalConfigurationService', function ($http) {
  this.getGlobalConfiguration = function () {

    // TODO get from db
    var path = 'scripts/model/globalConfiguration.json';
    return $http.get(path).then(function (response) {
      return response.data.globalConfiguration;
    });
  };

  this.saveGlobalConfiguration = function (globalConfiguration) {

    // TODO: save on db
    //var path = 'scripts/model/globalConfiguration.json';
    //return $http.post(path, globalConfiguration).then(function (response) {
    //  alert('global configuration was saved succesfully!');
    //});
  }

});

1 个答案:

答案 0 :(得分:0)

您可以注入$q服务并使用$q.when在返回时包装对象,这样您总是从api返回一个promise(并且只删除了多余的else)。还记得拒绝承诺的回调承诺(如果需要)。

'use strict';
angular.module('test').service('cachingGlobalConfigurationService', function (localStorageService, globalConfigurationService, $q) {
  this.getGlobalConfiguration = function () {

    var cachedGlobalConfiguration = localStorageService.get('globalConfiguration');
    if (cachedGlobalConfiguration) {
        //Return a promise
        return $q.when(cachedGlobalConfiguration);
     }

     return globalConfigurationService.getGlobalConfiguration().then(
        function (globalConfiguration) {
          localStorageService.set('globalConfiguration', globalConfiguration);
          return globalConfiguration;
        },
        function (error) {
          console.log('error', error);
          return $q.reject(error); //<-- reject
        });
  };

  //....
});
  

$ q.when - 将一个可能是值的对象或(第三方)包装成$ q保证。当您处理可能会或可能不是承诺的对象,或者承诺来自不可信任的来源时,这非常有用。