|
Reference
API Reference
Featured IntroductionUsing jQuery AOP plugin is very easy, just include the .js file in your code and add advices to your methods with a single call. API ReferenceAdd an advice before a methodbefore( Map pointcut, Function advice ) returns Array<Function> Creates an advice before the defined point-cut. The advice will be executed before the point-cut method but cannot modify the behavior of the method, or prevent its execution. This function returns an array of weaved aspects (Function). Parameters
Samples
jQuery.aop.before( {target: window, method: 'MyGlobalMethod'},
function() {
alert('About to execute MyGlobalMethod');
}
);
jQuery.aop.before( {target: window, method: /My/},
function() {
alert('About to execute one of my global methods');
}
);
jQuery.aop.before( {target: String, method: 'indexOf'},
function(index) {
alert('About to execute String.indexOf on: ' + this);
}
);
Add an advice after a methodafter( Map pointcut, Function advice ) returns Array<Function> Creates an advice after the defined point-cut. The advice will be executed after the point-cut method has completed execution successfully, and will receive one parameter with the result of the execution. This function returns an array of weaved aspects (Function). Parameters
Samples
jQuery.aop.after( {target: window, method: 'MyGlobalMethod'},
function(result) {
alert('Returned: ' + result);
return result;
}
);
jQuery.aop.after( {target: String, method: 'indexOf'},
function(index) {
alert('Result found at: ' + index + ' on:' + this);
return index;
}
);
Add an advice after a method throws an exceptionNew in version 1.3 afterThrow( Map pointcut, Function advice ) returns Array<Function> Creates an advice after the defined point-cut only for unhandled exceptions. The advice will be executed after the point-cut method only if the execution failed and an exception has been thrown. It will receive one parameter with the exception thrown by the point-cut method. This function returns an array of weaved aspects (Function). Parameters
Samples
jQuery.aop.afterThrow( {target: String, method: 'indexOf'},
function(exception) {
alert('Unhandled exception: ' + exception);
return -1;
}
);
jQuery.aop.afterThrow( {target: calculator, method: 'Calculate'},
function(exception) {
console.log('Unhandled exception: ' + exception);
throw exception;
}
);
Add an advice after a method regardless of the resultsNew in version 1.3 afterFinally( Map pointcut, Function advice ) returns Array<Function> Creates an advice after the defined point-cut. The advice will be executed after the point-cut method regardless of its success or failure, and it will receive two parameters: one with the result of a successful execution or null, and another one with the exception thrown or null. This function returns an array of weaved aspects (Function). Parameters
Samples
jQuery.aop.afterFinally( {target: window, method: 'MyGlobalMethod'},
function(result, exception) {
if (exception == null)
return 'Returned: ' + result;
else
return 'Unhandled exception: ' + exception;
}
);
Add an advice around a methodaround( Map pointcut, Function advice ) returns Array<Function> Creates an advice around the defined point-cut. This type of advice can control the point-cut method execution by calling the functions .proceed() on the invocation object, and also, can modify the arguments collection before sending them to the function call. This function returns an array of weaved aspects (Function). Parameters
Samples
jQuery.aop.around( {target: window, method: 'MyGlobalMethod'},
function(invocation) {
alert('# of Arguments: ' + invocation.arguments.length);
return invocation.proceed();
}
);
jQuery.aop.around( {target: String, method: 'indexOf'},
function(invocation) {
alert('Searching: ' + invocation.arguments[0] + ' on: ' + this);
return invocation.proceed();
}
);
jQuery.aop.around( {target: window, method: /Get(\d+)/},
function(invocation) {
alert('Executing method ' + invocation.method);
return invocation.proceed();
}
);
Add a new method using introductionsNew in version 1.1 introduction( Map pointcut, Function advice ) returns Array<Function> Creates an introduction on the defined point-cut. This type of advice replaces any existing methods with the same name. To restore them, just unweave it. This function returns an array with only one weaved aspect (Function). Parameters
Samples
jQuery.aop.introduction( {target: String, method: 'log'},
function() {
alert('Console: ' + this);
}
);
Removing advicesfn.unweave(); Advices can be removed after being applied. Just keep a reference to the return values of the function used to add the advice, and invoke the unweave() method. Samples
var advices = jQuery.aop.after( {target: String, method: 'indexOf'},
function(index) {
alert('Result found at: ' + index + ' on:' + this);
}
);
// Remove the advice
advices[0].unweave();
|
► Sign in to add a comment
very nice
i'm lovin this thing!
love that :)
2 years later, still the best in class. Great work, will use it (for performance logging)
Looks nice! I'm going to give it a try! Thanx!