summaryrefslogtreecommitdiffstats
path: root/js/vendor/ev-emitter/README.md
blob: 06c642d045fc928285606c14fe4c7291fd879c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# EvEmitter

_Lil' event emitter_ — add a little pub/sub

EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of [Olical/EventEmitter](https://github.com/Olical/EventEmitter). That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like [Isotope](http://isotope.metafizzy.co), [Flickity](http://flickity.metafizzy.co), [Masonry](http://masonry.desandro.com), and [imagesLoaded](http://imagesloaded.desandro.com).

## API

``` js
// Inherit prototype, IE8+
MyClass.prototype = new EvEmitter();

// Inherit prototype, IE9+
MyClass.prototype = Object.create( EvEmitter.prototype );

// Mixin prototype
_.extend( MyClass.prototype, EvEmitter.prototype );

// single instance
var emitter = new EventEmitter();
```

### on

Add an event listener.

``` js
emitter.on( eventName, listener )
```

+ `eventName` - _String_ - name of the event
+ `listener` - _Function_

### off

Remove an event listener.

``` js
emitter.off( eventName, listener )
```

### once

Add an event listener to be triggered only once.

``` js
emitter.once( eventName, listener )
```

### emitEvent

Trigger an event.

``` js
emitter.emitEvent( eventName, args )
```

+ `eventName` - _String_ - name of the event
+ `args` - _Array_ - arguments passed to listeners

## Code example

``` js
// create event emitter
var emitter = new EventEmitter();

// listeners
function hey( a, b, c ) {
  console.log( 'Hey', a, b, c )
}

function ho( a, b, c ) {
  console.log( 'Ho', a, b, c )
}

function letsGo( a, b, c ) {
  console.log( 'Lets go', a, b, c )
}

// bind listeners
emitter.on( 'rock', hey )
emitter.once( 'rock', ho )
// trigger letsGo once
emitter.on( 'rock', letsGo )

// emit event
emitter.emitEvent( 'rock', [ 1, 2, 3 ] )
// => 'Hey', 1, 2, 3
// => 'Ho', 1, 2, 3
// => 'Lets go', 1, 2, 3

// unbind
emitter.off( 'rock', ho )

emitter.emitEvent( 'rock', [ 4, 5, 6 ] )
// => 'Hey' 4, 5, 6
```

## License

EvEmitter is released under the [MIT License](http://desandro.mit-license.org/). Have at it.