peggy_Lin
peggy_Lin

Reputation: 47

How to emit an event from another js file?

This is a example of EventEmitter.

var events = require('events'); 
var emitter = new events.EventEmitter(); 
emitter.on('someEvent', function(arg1, arg2) { 
    console.log('listener1', arg1, arg2); 
}); 
emitter.emit('someEvent', 'byvoid', 1991); 

How to separate emitter.on() to another js file?

Upvotes: 2

Views: 2262

Answers (1)

Clément Berthou
Clément Berthou

Reputation: 1948

It depends on what you want to do, but if you want to go for an event centered programming pattern, you may as well create an EventEmitter.js file, which would look like that :

var EventEmitter = require('events').EventEmitter;
var localEmitter = new EventEmitter();

module.exports = localEmitter;

Then, you can just access your EventEmitter instance with require('EventEmitter.js').

Upvotes: 1

Related Questions