I'm struggling with this for almost 2hours and still not getting it.=( For explanation I got the following code.
var util = require('util');
var events = require('events').EventEmitter;
function a (){
//this is my first function
var b_obj = new b();
b_obj.on('event',function(){
console.log('Something happened!');
});
};
function b (){
//this is my second function
events.call(this);
this.emit('event');
};
util.inherits(b, events);
a();
So what I try to do is: I got a function called "a". This function calls a second function called "b". This function validates some data and emits events depending on the result of the validation.
What I found out is, that the listener works fine and the event is correct emitted. But, as it looks like the event is emitted in the context of function b and the listener works in the context of function a. For further investigations I added the following line to function b:
this.on('event', function(){
console.log('event emitted!');
});
And this works. Can anybody help me? I'm sure the solution is pretty simple. =(