javascript - angularjs:我可以合并这项服务吗?

标签 javascript angularjs

我有两个需要加载的 JSON 文件。我现在通过单独的服务加载它们,如下所示:

app.factory('AventosService', function($rootScope, $http)
{
  var data = [];
  return {
    promise: null,
    loadAventosJson: function()
    {
      this.promise = $http.get('resources/json/aventos.json',{timeout:20000}).then(
      function(response)
      {
        data = response.data;
        $rootScope.$broadcast('AventosJsonLoaded');
      },
      function(data)
      {
        log('ERROR: ' + data.status);
      })
    },
    getAventosJson: function()
    {
      if (!data.length && !this.promise) this.loadAventosJson();
      return data;
    }
  }
});

app.factory('PartsService', function($rootScope, $http)
{
  var data = [];
  return {
    promise: null,
    loadPartsJson: function()
    {
      this.promise = $http.get('resources/json/part_numbers.json',{timeout:20000}).then(
      function(response)
      {
        data = response.data;
        $rootScope.$broadcast('PartsJsonLoaded');
      },
      function(data)
      {
        log('ERROR: ' + data.status);
      })
    },
    getPartsJson: function()
    {
      if (!data.length && !this.promise) this.loadPartsJson();
      return data;
    }
  }
});

要调用该服务,我只需执行以下操作:

$scope.aventosJson = AventosService.getAventosJson();

和一个

$scope.partsJson = PartsService.getPartsJson();

然后我检查这两个事件是否都发生了火灾。这两个事件都是 AventosJsonLoadedPartsJsonLoaded

最佳答案

给你:

app.factory('TheService', function($rootScope, $http)
{
  var data = [];
  return {
    aventosPromise:null,
    loadAventosJson: function()
    {
      this.aventosPromise = $http.get('resources/json/aventos.json',{timeout:20000}).then(
      function(response)
      {
        data = response.data;
        $rootScope.$broadcast('AventosJsonLoaded');
      },
      function(data)
      {
        log('ERROR: ' + data.status);
      })
    },
    getAventosJson: function()
    {
      if (!data.length && !this.aventosPromise) this.loadAventosJson();
      return data;
    },
    partsPromise: null,
    loadPartsJson: function()
    {
      this.partsPromise = $http.get('resources/json/part_numbers.json',{timeout:20000}).then(
      function(response)
      {
        data = response.data;
        $rootScope.$broadcast('PartsJsonLoaded');
      },
      function(data)
      {
        log('ERROR: ' + data.status);
      })
    },
    getPartsJson: function()
    {
      if (!data.length && !this.partsPromise) this.loadPartsJson();
      return data;
    }
  }
});

关于javascript - angularjs:我可以合并这项服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18392199/

相关文章:

angularjs - 没有 done() 方法, Protractor 无法通过我的测试

javascript - Angular源码中的正则表达式

javascript - 是否可以在 TypeScript 注释中组合多种类型的成员?

javascript - 在谷歌地图API上用ID覆盖标记点

javascript - 如何在 JavaScript 文件中导入 json 文件?

javascript - 在 javascript 中使用 Selenium Driver 获取 WebElement 文本内容

javascript - 如何将js类包装到短信中?

javascript - 如何将 Angular forEach 与 firebase 一起使用

javascript - 从 Angular Controller 发布到 Node

internet-explorer - SCRIPT5022 : 10 $digest() iterations reached. 正在中止!并重定向到index.html