Skip to content

A trait to add method advices (after, before, around) experimental

Notifications You must be signed in to change notification settings

CocktailJS/lab-trait-advisable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advisable Trait (Experimental)

A CocktailJS Trait Extension

This trait experiment aims to add AOP advices into Classes. The methods around, after and before are available on host classes or objects.

See example.js file for some examples.

Update

This experiment has been moved to https://github.com/CocktailJS/cocktail-trait-advisable repository and it is being maintained there.

Install

npm install git://github.com/CocktailJS/lab-trait-advisable.git --save

Usage

Once dependency is installed, the trait module name is a bit different.

Test.js

var cocktail  = require('cocktail'),
    Advisable = require('cocktail-trait-advisable');

cocktail.mix({
    '@exports': module,
    '@as': 'class',

    '@traits': [Advisable],

    constructor: function(options) {
        var after = options.after;
        this.param = options.param;
        
        if (after){
            this.after(after.method, after.advice);
        }

        this.initialize();
    },

    initialize: function() {
        console.log('Test initialize');
    },

    get: function() {
        console.log('Test get');
    }
});

Note: The module name is cocktail-trait-advisable just to follow the name convention for CocktailJS extensions.

Then in index.js we can use our Test.js class:

index.js

var Test = require('./Test'),
    test;


var test = new Test({
    param: 'param',
    after: {
        method: 'get',
        advice: function() {
            console.log('test after get');
        }
    }
});

test.get();

Try the example

Clone this repo and install dependencies

git clone [email protected]:CocktailJS/lab-trait-advisable.git 
cd lab-trait-advisable
npm install

Now just execute example.js

node example.js

About

A trait to add method advices (after, before, around) experimental

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published