Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cocktail-trait-advisable

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cocktail-trait-advisable

A CocktailJS Trait Extension to add AOP methods to the host class

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status npm version

cocktail-trait-advisable

A CocktailJS Trait Extension

A 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 --save
npm install cocktail-trait-advisable --save

Trait requires (glue code)

None.

Usage

Define a class with advisable trait:

MyClass.js


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

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

    '@traits' : [advisable],

    aMethod: function () {
        console.log('a method is called!');
    }
});


And then use it on your index.js

index.js


var MyClass = require('./MyClass');



var obj = new MyClass();

function afterFn() { console.log('this will be called after!'); }

// #1 attach the after advice
obj.after('aMethod', afterFn);


// #2 call the adviced method
obj.aMethod(); 

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.js
a method is called
this 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.

Keywords

FAQs

Package last updated on 28 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc