工厂 $resource 中的公共 transformResponse

Common transformResponse in factory $resource

我们正在使用工厂与围绕常见实体的一系列自定义 API 进行交互。但是,我需要做的部分工作是评估 headers 中发回的自定义响应,看起来他们这样做的方法是使用 transformResponse。我所拥有的似乎在起作用,但我实际上是在每个定义中一遍又一遍地重复同样的事情。我尝试创建一个函数以使其可重用,但引用似乎失败了。我做错了什么?

(function()
{
    'use strict';

    angular.module('aumBills', ['ngResource'])
    .factory('Bills', ['$resource',  
    function($resource)
    {
        return $resource(
        '/ua_aumcore/bills/api/v1/bills/:billableEventI',
        {
            billableEventI:'@billableEventI'
        },
        {
            getList:
            {
                method: 'GET',
                isArray: false,
                transformResponse: function(data, header, status, config, statusText)
                {
                    var response = {};
                    if (isJson(data))
                    {
                        data = angular.fromJson(data);
                        response.data = data;
                    }
                    response.status = status;
                    response.config = config;
                    response.statusText = statusText;
                    return response;
                },
                url: '/ua_aumcore/bills/api/v1/bills/query/'
            },
            getParties:
            {
                method: 'GET',
                isArray: false,
                transformResponse: function(data, header, status, config, statusText)
                {
                    var response = {};
                    if (isJson(data))
                    {
                        data = angular.fromJson(data);
                        response.data = data;
                    }
                    response.status = status;
                    response.config = config;
                    response.statusText = statusText;
                    return response;
                },
                url: '/ua_aumcore/bills/api/v1/customer/billParties/?partySites=:partySiteIDs', 
                params: {partySiteIDs: '@partySiteIDs'}
            }
            //plus about 12 more after this         
        });
    }]);

    function isJson(str)
    {
        try
        {
            JSON.parse(str);
        }
        catch (e)
        {
            return false;
        }
        return true;
    }
})();

破解的代码看起来与上面相同,只是我输入:

transformResponse: transResp(data, header, status, config, statusText),

在每个定义中,然后是紧跟在isJSON之后的函数:

function transResp(data, header, status, config, statusText)
    {
        var response = {};
        if (isJson(data))
        {
            data = angular.fromJson(data);
            response.data = data;
        }
        response.status = status;
        response.config = config;
        response.statusText = statusText;
        return response;
    }

似乎定义了我想用作变量的 transformResponse 函数,并将该变量插入到定义中有效:

(function()
{
    'use strict';

    angular.module('aumBills', ['ngResource'])
    .factory('Bills', ['$resource',
    function($resource)
    {
        var transResp = function(data, header, status, config, statusText)
        {
            var response = {};
            if (isJson(data))
            {
                data = angular.fromJson(data);
                response.data = data;
            }
            response.status = status;
            response.config = config;
            response.statusText = statusText;
            return response;
        };

        return $resource(
        '/ua_aumcore/bills/api/v1/bills/:billableEventI',
        {
            billableEventI:'@billableEventI'
        },
        {
            get:
            {
                method: 'GET',
                isArray: false,
                transformResponse: transResp,
                url: '/ua_aumcore/bills/api/v1/bills/:billableEventI'
            },
            getList:
            {
                method: 'GET',
                isArray: false,
                transformResponse: transResp,
                url: '/ua_aumcore/bills/api/v1/bills/query/'
            }
            //and so on and so forth
        });
    }]);

    function isJson(str)
    {
        try
        {
            JSON.parse(str);
        }
        catch (e)
        {
            return false;
        }
        return true;
    }
})();