Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.
Wolfy87 edited this page Sep 1, 2012 · 14 revisions

WARNING

This documentation is for the old version 3 of EventEmitter. You will probably want the API documentation or guide for version 4+.

Introduction

This wiki is dedicated to EventEmitter. A JavaScript based event manager that can run in almost any browser or server based JavaScript environment extremely quickly and with minimal fuss. Here you will find documentation and examples among other useful things.

Download

You can either download the zip of the whole project or just grab one of the source files.

The project is compressed with the UglifyJS and validated with JSHint.

Quickstart

For full documentation simply scroll down a bit, but you are obviously in a hurry so I will try to get you up and running quickly in this section.

First things first, you need to include the script. In a browser I recommend the minified version and the following line above any code that uses EventEmitter.

<script type='text/javascript' src='/assets/javascript/EventEmitter.min.js'></script>

If how ever you are using it as a replacement for node.js's EventEmitter I recommend the uncompressed version, you can include it like so.

var EventEmitter = require('./lib/EventEmitter').EventEmitter;

Now, you can either create a new instance of EventEmitter and use it like this.

var ee = new EventEmitter();
ee.on('foo', barFunction);

Or you can extend EventEmitter with your own class. This means your class will have access to all of the EventEmitter methods as well as it's own.

function myEpicClass() {
    // We need to initialise EventEmitter first
    EventEmitter.apply(this);
    
    // Your class initialisation code goes here
}

// Copy in EventEmitters prototype to give your class all of the EventEmitter methods
// We use the new extend method to clone the prototype, more information can be found here:
// https://github.com/Wolfy87/EventEmitter/issues/25
myEpicClass.prototype = EventEmitter.extend();

// Add a method to your class
myEpicClass.prototype.methodName = function() {
    // Your method code here
};

After initialising like the above code you can then do things like this.

var test = new myEpicClass();
test.on('foo', function() {
    console.log('bar');
});
test.methodName();
test.emit('foo');

You can play about with extending the class in this fiddle.

Documentation

The following links go to pages describing all of EventEmitter's methods in detail. The layout of the methods is EventEmitter.method. You do not actually type EventEmitter, you must first create an instance of it and then call from that. Like so.

var ee = new EventEmitter();
ee.addListener('foo', function() {
    console.log('bar');
});