Created
June 26, 2014 11:29
-
-
Save oranheim/e879eb581c9fd87b2b1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* http://book.mixu.net/node/ch9.html | |
* | |
* Created by ora on 27.03.14. | |
*/ | |
var SimpleEE = function () { | |
this.events = {}; | |
}; | |
SimpleEE.prototype.on = function (eventname, callback) { | |
this.events[eventname] || (this.events[eventname] = []); | |
this.events[eventname].push(callback); | |
}; | |
SimpleEE.prototype.emit = function (eventname) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
if (this.events[eventname]) { | |
this.events[eventname].forEach(function (callback) { | |
callback.apply(this, args); | |
}); | |
} | |
}; | |
describe('Test EventEmitter', function () { | |
describe('Call ee', function () { | |
it('should pass ee callback', function () { | |
// Example using the event emitter | |
var emitter = new SimpleEE(); | |
emitter.on('greet', function (name) { | |
console.log('Hello, ' + name + '!'); | |
}); | |
emitter.on('greet', function (name) { | |
console.log('World, ' + name + '!'); | |
}); | |
['foo', 'bar', 'baz'].forEach(function (name) { | |
emitter.emit('greet', name); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment