工厂$资源中的常见transformResponse

时间:2016-06-01 20:54:23

标签: angularjs angular-resource angular-http

我们正在使用工厂与一系列围绕公共实体的自定义API进行交互。但是,我需要做的部分是评估在标头中发回的自定义响应,看起来他们这样做的方法是使用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;
    }

1 个答案:

答案 0 :(得分:1)

看起来像定义我想用作变量的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;
    }
})();