# cocktail-trait-eventable

> EventEmitter as delegate trait

Latest version **0.0.7** (published 2016-03-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install cocktail-trait-eventable
pnpm add cocktail-trait-eventable
yarn add cocktail-trait-eventable
bun add cocktail-trait-eventable
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2016-03-24 |
| First published | 2013-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Maximiliano Fierro |
| Maintainers | elmasse |
| Keywords | eventable, event, trait, cocktailjs |

## Links

- npm: https://www.npmjs.com/package/cocktail-trait-eventable
- Repository: https://github.com/CocktailJS/cocktail-trait-eventable
- Homepage: https://github.com/CocktailJS/cocktail-trait-eventable#readme
- Issues: https://github.com/CocktailJS/cocktail-trait-eventable/issues
- npm.io page: https://npm.io/package/cocktail-trait-eventable

## Dependencies (1)

- [cocktail](https://npm.io/package/cocktail.md) >=0.5.1 <0.8.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.0.7 (latest) — 2016-03-24
- 0.0.6 — 2014-06-12
- 0.0.5 — 2013-12-03
- 0.0.4 — 2013-12-03
- 0.0.3 — 2013-11-01
- 0.0.2 — 2013-10-27
- 0.0.1 — 2013-10-25

## README

[![Build Status](https://travis-ci.org/CocktailJS/cocktail-trait-eventable.png?branch=master)](https://travis-ci.org/CocktailJS/cocktail-trait-eventable)
[![NPM version](https://badge.fury.io/js/cocktail-trait-eventable.png)](http://badge.fury.io/js/cocktail-trait-eventable)

# cocktail-trait-eventable
## A [CocktailJS](http://cocktailjs.github.io) Trait Extension

A trait to use an Emitter as a delegate.

### Install

````bash
npm install cocktail --save
npm install cocktail-trait-eventable --save
````

### Trait requires (glue code)

A `getEmitter` method is required as a **glue code** for this trait.

`getEmitter()`: should return an EventEmitter instance or any other object with the same api as node `events.EventEmitter` class.

### Usage

MyClass.js

````javascript
var cocktail     = require('cocktail'),
    Eventable    = require('cocktail-trait-eventable'),
    EventEmitter = require('events').EventEmitter;

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

    '@traits' : [Eventable],

    constructor: function() {
        this._emitter = new EventEmitter();
    },

    // glue code for Eventable Trait
    getEmitter: function() {
        return this._emitter;
    },

    doSomethingAndFireEvent: function(){
        // ... do something here
        // and
        // now fire an event 'firing'
        this.emit('firing', this);
    }
});
````

And then in your index you can do:

index.js

````javascript

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

obj = new MyClass();

//we can call `on`  or `addListener` here since our MyClass is Eventable
obj.on('firing', function(){ console.log('Event Fired!'); });

obj.doSomethingAndFireEvent();
````

### API

The following methods will be publicly available on the host class:

- `addListener(eventName, handler)`: Adds a listener to the end of the listeners array for the specified event.
    - **eventName**: {String} the event name to be listened to.
    - **handler**: {Function} the event handler.

````javascript
    myObj.addListener('event', function(){/*handler*/});
````

- `addListener(options)`: Adds a listener to the end of the listeners array for the specified event.
    - **options**: {Object} an object containing value-pairs of event name and handler and an optional scope.

````javascript
    myObj.addListener({event: myObj.onEvent, scope: myObj});
````

- `emit(eventName, arg1, arg2, ..., argN)`: Execute each of the listeners in order with the supplied arguments.
    - **eventName**: {String} the event name to be listened to.
    - **arg1..N**: {Any} arguments to be passed as parameters in the event handler.

````javascript
    myObj.emit('event', myObj, true, 1, 'another param');
````

- `on(eventName, handler)`: Idem `addListener` method.

````javascript
    myObj.on('event', function(){/*handler*/});
````

- `on(options)`: Idem `addListener` method.

````javascript
    myObj.on({event: myObj.onEvent, scope: myObj});
````

- `once(eventName, handler)`: Adds a one time event listener.
    - **eventName**: {String} the event name to be listened to.
    - **handler**: {Function} the event handler.

````javascript
    myObj.once('event', function(){/*handler*/});
````

- `once(options)`: Adds a one time event listener.
    - **options**: {Object} an object containing value-pairs of event name and handler and an optional scope.

````javascript
    myObj.once({event: myObj.onEvent, scope: myObj});
````

- `removeListener(eventName, handler)`: Removes the given handler for the event.
    - **eventName**: {String} the event name.
    - **handler**: {Function} the event handler.

````javascript
    myObj.removeListener('event', funcHandler);
````

- `removeListener(options)`: Removes the given handler for the event.
    - **options**: {Object} an object containing value-pairs of event name and handler.

````javascript
    myObj.removeListener({event: myObj.onEvent});
````

- `removeAllListeners(eventName)`: Remove all listeners for the given event.
    - **eventName**: {String} the event name.

````javascript
    myObj.removeAllListeners();
````

---
_Source: https://npm.io/package/cocktail-trait-eventable · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
