通用工厂/服务,以避免Angular

时间:2017-01-26 12:46:14

标签: javascript angularjs duplicates refactoring factory

所以我有一套看起来非常相似的工厂。我想制作一个能减少重复代码的通用函数。

代码示例:

.factory('neQueryService', function ($http, $resource, constants) {
return $resource(constants.GEODATA_QUERY, {},
  {
    get: {
      method: 'GET',
      transformResponse: function(data, headers, status){
        var response = {};
        response.data = data;
        response.headers = headers();
        response.status = status;
        return response;
      }
    }
  }
);

.factory('neTaskService', function ($http, $resource, constants) {
return $resource(constants.GEODATA_TASK, {},
  {
    get: {
      method: 'GET',
      transformResponse: function(data, headers, status){
        var response = {};
        response.data = data;
        response.headers = headers();
        response.status = status;
        return response;
      }
    }
  }
);

正如你所看到的,来自" get"到"返回"我在工厂之间是一样的。是否有一个聪明的"最佳实践"可能会将该部分放入一个单独的函数中,并在所有工厂中引用它?

3 个答案:

答案 0 :(得分:3)

您可以使用value()提供商,因为它没有任何外部依赖关系:

.value('resourceConfig', {
    get: {
      method: 'GET',
      transformResponse: function(data, headers, status){
        var response = {};
        response.data = data;
        response.headers = headers();
        response.status = status;
        return response;
      }
    }
});

.factory('neQueryService', function ($http, $resource, constants, resourceConfig) {
    return $resource(constants.GEODATA_QUERY, {}, resourceConfig);
});

.factory('neTaskService', function ($http, $resource, constants, resourceConfig) {
    return $resource(constants.GEODATA_TASK, {}, resourceConfig);
});

或者您可以更进一步,将工厂分解为创建资源:

.factory('prepareResource', function ($resource) {
    return function (query) {
        return $resource(query, {}, {
            get: {
                method: 'GET',
                transformResponse: function(data, headers, status){
                    var response = {};
                    response.data = data;
                    response.headers = headers();
                    response.status = status;
                    return response;
                }
           }
       });
   };
});

.factory('neQueryService', function (prepareResource, constants) {
    return prepareResource(constants.GEODATA_QUERY);
});

.factory('neTaskService', function (prepareResource, constants) {
    return prepareResource(constants.GEODATA_TASK);
});

答案 1 :(得分:1)

如果您不使用ES6模块,只需将公共代码移动到另一个工厂:

.factory('GetQuery', function () {
  return {
    method: 'GET',
    transformResponse: function(data, headers, status){
      var response = {};
      response.data = data;
      response.headers = headers();
      response.status = status;
      return response;
    }
  });

.factory('neQueryService', function ($http, $resource, constants, GetQuery) {
  return $resource(constants.GEODATA_QUERY, {},
    {
      get: GetQuery
    }
);

答案 2 :(得分:1)

或者,在$ resourceProvider中配置默认​​操作:

app.config(function($resourceProvider) {
    $resourceProvider.defaults.get = {
        method: 'GET',
        transformResponse: function(data, headers, status){
          var response = {};
          response.data = data;
          response.headers = headers();
          response.status = status;
          return response;
        }
     };
});

有关详细信息,请参阅AngularJS $resourceProvider API Reference.