我想在资源上创建自定义操作,它将处理从服务器收到的结果。
angular.module('problem', ['ngRoute', 'ngResource'])
.factory('Abc', function ($resource) {
return $resource('api/abc/:abcId', {abcId: '@id'}, {
customQuery: {
method: "GET",
isArray: true,
interceptor: {
response: function (response) {
// some operations that manipulate data based od response
response.data = [5, 6]; // for simplifity
console.log("manipulation finished"); // log is saved
return response;
}
}
}
}
);
})
;
但是当我使用自定义操作时,我得到未经修改的结果而不是处理。 以下是显示预期行为的代码(以及评论相关错误):
describe('Abc', function () {
beforeEach(module('problem'));
var $httpBackend;
beforeEach(function () {
angular.mock.inject(function ($injector) {
$httpBackend = $injector.get('$httpBackend');
})
});
it('should return converted array when customQuery called', inject(function (Abc) {
$httpBackend
.expectGET('api/abc')
.respond([
{id: 'uid1', name: 'name1'},
{id: 'uid2', name: 'name2'},
{id: 'uid3', name: 'name3'},
{id: 'uid4', name: 'name4'}
]);
var result = Abc.customQuery();
$httpBackend.flush();
expect(result.length).toBe(2); // fails with "Expected 4 to be 2."
expect(result[0]).toBe(5); // fails with "Expected { id : 'uid1', name : 'name1' } to be 5."
expect(result[1]).toBe(6); // fails with "Expected { id : 'uid2', name : 'name2' } to be 6."
}));
});
答案 0 :(得分:0)
在我看来
response.data = [5, 6]; // for simplifity
return response;
应该返回一个响应对象,其属性数组为“data”,所以
result.length;
应该失败。
为了操纵资源响应的结果,最好使用响应。资源而不是response.data - 有真正的REST对象(使用CRUD方法)
答案 1 :(得分:0)
也许你需要的是添加一个新的transformResponse转换,你可以轻松地这样做(记得注入$http
):
transformResponse: $http.defaults.transformResponse.concat([
function (data, headersGetter) {
return data.objects
}
区别在于,result
将被response.resource
替换,而拦截器的返回值是解析result.$promise
的值