我需要在每次获取,保存,更新时转换服务响应。我已经创建了资源并添加了要执行的转换器,但是返回的对象的结构与不使用转换器时的结构不同。在这里,我谈论的是响应的结构,而不是我要转换的对象。这是我的资源:
angular.module('app')
.factory('Insureds', ['$resource', 'config', function ($resource, config) {
function transform(response) {
var insured = response.data.insured;
return response;
}
var memberServicesHostName = config.memberServicesHostName;
return $resource(memberServicesHostName + '/insureds/:insuredId', null,
{
'get': {
method: 'GET', 'withCredentials': true, interceptor:
{
response: function (response) { return transform(response).data; }
}
},
'update': { method: 'PUT', 'withCredentials': true },
'save': { method: 'POST', 'withCredentials': true }
});
}]);
当不使用承诺时,当我不使用转换器“ insured”处于第一级时,它将解析为被保险对象的实例。但是在转换器中,有一个包装对象,其中包含被保险人和responseStatus属性。这可能与我从拦截器中的“响应”返回的内容有关。一个回传,原始回复(例如我正在做的事情),response.data或response.resource.insured应该是什么?我很迷惑...
默认的响应拦截器是这样的:
function defaultResponseInterceptor(response) {
return response.resource;
}
因此,如果您想保留默认行为,则必须返回response.resource
而不是response.data
:
return $resource(memberServicesHostName + '/insureds/:insuredId', null, {
get: {
method: 'GET',
withCredentials: true,
interceptor: {
response: function (response) {
return transform(response).resource;
}
}
},
...
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句