我目前正在使用角度仇恨(https://github.com/jmarquis/angular-hateoas)。我想在HateoasInterface中创建的资源的query()和get()函数中添加特定的拦截器。我一直在寻找方法,但没有成功。
我认为可以通过这样添加它来完成:
var someResource = someService.resource('someresource');
someResource.query.interceptors = {
response: function (data) {
// do something data
return data
},
responseError: function (error) {
// do something with error
return $q.reject(error);
}
};
但这给了我:
TypeError: Attempted to assign to readonly property.
我可能需要使用$ decorator,但我没有经验,我没有看到将特定拦截器添加到特定资源对象的示例。
我真的不想使用$httpProvider.interceptors
,因为我不希望拦截器在所有资源上工作。
我目前唯一能想到的是,使用包含特定拦截器的特定命名函数来配置HateoasInterfaceProvider。
angular.module('myModule')
.config(HateoasInterfaceConfig);
HateoasInterfaceConfig.$inject = ['HateoasInterfaceProvider'];
function HateoasInterfaceConfig(HateoasInterfaceProvider) {
HateoasInterfaceProvider.setHttpMethods({
get: {
method: 'GET',
isArray: false
},
getSomeResource: {
method: 'GET',
isArray: false,
interceptors: {
response: someResponseFunc,
responseError: someErrorFunc
}
},
update: {
method: 'POST',
},
query: {
method: 'GET',
isArray: true
}
querySomeResource: {
method: 'GET',
isArray: true,
interceptors: {
response: function(data) {
// do something with data
return data;
},
responseError: function (error) {
//do something with error
return $q.reject(error);
}
}
});
HateoasInterfaceProvider.setLinksKey('_links');
}
但我不喜欢那样做。
弄清楚了。
调用资源时,可以传递参数和动作。
所以喜欢:
someServiceResult.resource('someresource',{},{get: {method: 'GET',...,
interceptor: { response: responseInterceptorFunc, ...}}})
仍然不是真正的首选解决方案,但当包含在服务中的功能时,可以加入。
我想有一个解决方案,允许更改创建的Resource对象的拦截器定义:
someServiceResult.resource('someresource')
但我目前没有时间搞清楚这一点。