angularjs AOP机制
AOP(Aspect-Oriented Programming),面向切面编程。
通过compile时植入代码,runtime时动态代理,以及框架提供管道式执行等策略实现程序通用功能与业务模块的分离,统一处理,维护的一种解耦设计。
AOP是OOP的延续,是函数式编程的一种衍生泛型。+
AOP实用场景有:权限控制,日志模块,事务处理,性能统计,异常处理等独立,通用的非业务模块。+
下面是Angular中两类常见的AOP切入点。
- http拦截器(管道式策略)
//定义拦截器
angular.module('myApp')
.factory('myInterceptor',
function($q) {
var interceptor={
'request': request,
'response':response,
'requestError':requestError,
'responseError':responseError
}
return interceptor;
function request(config){
// Successful request method
return config; // or $q.when(config);
}
function response(response){
return response; // or $q.when(response);
}
function requestError(rejection){
return $q.reject(rejection);
}
function responseError(rejection){
// an error happened on the request if we can recover from the error,
// we can return a new response or promise
return rejection; // or new promise
// Otherwise, we can reject the next by returning a rejection
// return $q.reject(rejection);
}
// 使用拦截器
angular.module('myApp')
.config(function($httpProvider) {
$httpProvider.interceptors.push('myInterceptor');
});
拦截器提供了对发出请求到返回响应的全生命周期处理,一般可以用来做下面几个事情:
1.统一在发出的请求中添加数据,如添加身份验证信息
2.统一处理错误,包括请求发出时出的错(如浏览器端的网络不通),还有响应时返回的错误
3.统一处理响应,比如缓存一些数据等
4.显示请求进度条
其实,只要服务器返回的状态码不是 200 ,都会调用 responseError ,可以在这里,统一处理并显示错误。