node package manager

cocktail-annotation-evented

Build Status NPM version

cocktail-annotation-evented

A CocktailJS Annotation Extension

This extension defines a custom annotation to apply Eventable Trait and creates the required glue code into the host class.

Install

npm install cocktail --save
npm install cocktail-annotation-evented --save

Requirements

CocktailJS v0.5.0 or greater is required to use this annotation.

Usage

Using @evented annotation is quite easy. First we need to register the custom annotation with current cocktail instance.

MyClass.js

var cocktail = require('cocktail'),
    Evented  = require('cocktail-annotation-evented');
 
//register Evented annotation with current cocktail instance 
cocktail.use(Evented);
 
cocktail.mix({
    '@exports': module
    '@as'     : 'class',
 
    // we can say that our class is evented by passing `true` as a param 
    '@evented': true,
 
    doSomething: function(){
        this.emit('doingSomething', this);
    } 
});

index.js

 
var MyClass = require('./MyClass'),
    obj;
 
obj = new MyClass();
 
obj.on('doingSomething', function(){console.log('obj is doing something!');});
 
obj.doSomething();
 

API

  • @evented: parameter
    • parameter: {boolean} if the parameter is true, the evented annotation will create and add a new instance of node's events.EventEmitter.