javascript - Sharing event across modules - Stack Overflow

For simplicity I have create some small files to test out the idea. Basically i'm looking for a wa

For simplicity I have create some small files to test out the idea. Basically i'm looking for a way to emit and event on a module then have that picked up back in the main file for example

This is the main file called to run the app

main.js

var test = require('./test')
var emitter = require('./emitter')
var emit = new emitter()

emit.on('response', function(data) {
    console.log(data)
})

This is where the event is fired

fire.js

var emitter = require('./emitter')
var emit = new emitter

var Test = function() {
    emit.sendEvent('response', 'test')
}


module.exports = Test

Here is the module created to handle the firing of events for the application

emitter.js

var util = require('util')
var eventEmitter = require('events').EventEmitter

function Event () {
    eventEmitter.call(this)
}

util.inherits(Event, eventEmitter)

Event.prototype.sendEvent = function(type, data) {
    this.emit(type, data)
}

module.exports = Event

As can be seen in the emitter.js I have it setup to be able to emit any custom type including data parsed.

I cannot seem to pick this up in the initial main.js file. However if I place emit.sendEvent('response', 'banana') in the main.js at the bottom of the file it is picked up by the listener above. This would indicate that my emitter.js is working correctly.

Any ideas as to why the event emitted in fire.js isn't being picked up would be great.

For simplicity I have create some small files to test out the idea. Basically i'm looking for a way to emit and event on a module then have that picked up back in the main file for example

This is the main file called to run the app

main.js

var test = require('./test')
var emitter = require('./emitter')
var emit = new emitter()

emit.on('response', function(data) {
    console.log(data)
})

This is where the event is fired

fire.js

var emitter = require('./emitter')
var emit = new emitter

var Test = function() {
    emit.sendEvent('response', 'test')
}


module.exports = Test

Here is the module created to handle the firing of events for the application

emitter.js

var util = require('util')
var eventEmitter = require('events').EventEmitter

function Event () {
    eventEmitter.call(this)
}

util.inherits(Event, eventEmitter)

Event.prototype.sendEvent = function(type, data) {
    this.emit(type, data)
}

module.exports = Event

As can be seen in the emitter.js I have it setup to be able to emit any custom type including data parsed.

I cannot seem to pick this up in the initial main.js file. However if I place emit.sendEvent('response', 'banana') in the main.js at the bottom of the file it is picked up by the listener above. This would indicate that my emitter.js is working correctly.

Any ideas as to why the event emitted in fire.js isn't being picked up would be great.

Share Improve this question asked Jun 20, 2016 at 18:31 Matt StephensMatt Stephens 9221 gold badge7 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

This is because "emit" object in fire.js and "emit" object in main.js are two pletely different objects, so subscribing to events from one will not catch events from another.

What you can do is to export global emitter and use it everywhere. For example:

emitter.js

var util = require('util')
var eventEmitter = require('events').EventEmitter

function Event () {
    eventEmitter.call(this)
}

util.inherits(Event, eventEmitter)

Event.prototype.sendEvent = function(type, data) {
    this.emit(type, data)
}
var eventBus = new Event();
module.exports = {
 emitter : Event,
 eventBus : eventBus
};

This way you can use global event object in all of your modules :

main.js

var test = require('./test')
var emitter = require('./emitter')

emitter.eventBus.on('response', function(data) {
    console.log(data)
})

fire.js

var emitter = require('./emitter')

var Test = function() {
    emitter.eventBus.sendEvent('response', 'test')
}


module.exports = Test

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308295a4567825.html

相关推荐

  • javascript - Sharing event across modules - Stack Overflow

    For simplicity I have create some small files to test out the idea. Basically i'm looking for a wa

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信