cocktail-trait-advisable
CocktailJS Trait Extension
AA Trait to add AOP advices into Classes/Objects.
The methods around
, after
and before
are available on host classes or objects.
Install
npm install cocktail --savenpm install cocktail-trait-advisable --save
Trait requires (glue code)
None.
Usage
Define a class with advisable trait:
MyClass.js
var cocktail = advisable = ; cocktail;
And then use it on your index.js
index.js
var MyClass = ; var obj = ; { console; } // #1 attach the after adviceobj; // #2 call the adviced methodobj;
On #1 we attached the advice for after calling aMethod
method in our obj
. Then when #2 is executed the console output will show:
node index.jsa method is calledthis will be called after!
API
The following methods will be publicly available on the host class:
after( methodName, adviceFunction, [scope] )
: Adds the adviceFunction to be called after the method.- methodName {String}: The method name in the host class.
- adviceFunction {String|Function}: the function or the name of the function to be called. It receives the same parameters as the method.
- scope {Object} the scope to execute the adviceFunction. Optional.
before( methodName, adviceFunction, [scope] )
: Adds the adviceFunction to be called before the method.- methodName {String}: The method name in the host class.
- adviceFunction {String|Function}: the function or the name of the function to be called. It receives the same parameters as the method.
- scope {Object} the scope to execute the adviceFunction. Optional.
around( methodName, adviceFunction, [scope] )
: Adds the adviceFunction to be called around the method.- methodName {String}: The method name in the host class.
- adviceFunction {String|Function}: the function or the name of the function to be called. It receives the method as the first parameter and then same parameters.
- scope {Object} the scope to execute the adviceFunction. Optional.