2

Is it possible to replace this context when sending an event?

var that = {}, obj = new myType();

// how to pass `that` as the calling context?
obj.emit('myEvent', data);

obj.on('myEvent', function () {
    // I need it to arrive with `this`=`that`
});

I'm using the standard EventEmitter inheritance approach:

In the beginning of my type:

events.EventEmitter.call(this);

Following the type function:

myType.prototype.__proto__ = events.EventEmitter.prototype;

But that seems to override any emit with whatever context I specified inside myType function. And when I need a different this context, I cannot figure out how to send it.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • So to clarify, you want the `this` inside of the event handler to be an object that was chosen (passed) in the `emit` call? – Ray Toal Jun 06 '15 at 21:20
  • @RayToal, yes, I want a custom object to be passed as `this` context to arrive into `on` handler. – vitaly-t Jun 06 '15 at 21:21

1 Answers1

0

If you want to change context each time you send an event you can pass another object containing the context to the emit function. Then we can write a wrapper function which automatically binds the new context to the handler.

For example:

function withContext(fn) {
   return function(data, context) {
       return fn.call(that, data);
   }
}

// how to pass `that` as the calling context?
obj.emit('event_1', data, context_1);
obj.emit('event_2', data, context_2);


obj.on('event_1, withContext(handler_1));
obj.on('event_2', withContext(function(data) { 
  this === that // true;
});
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • Interesting, but it kind of contradicts the other information I found so far: http://stackoverflow.com/questions/8005563/nodejs-eventemitter-define-scope-for-listener-function – vitaly-t Jun 06 '15 at 21:45
  • Ok, I understand your code better now, and that is absolutely not what I asked for. – vitaly-t Jun 06 '15 at 21:54
  • it is not contradicting the answer you found. The problem is the `enventEmitter` automatically is bound to the listener function. My wrapper is binding your handler to any object you pass to the emitter as second argument. However, I *strongly* recommend not to use that solution because what is doing is completely undocumented https://nodejs.org/api/events.html#events_emitter_emit_event_arg1_arg2 – Giuseppe Pes Jun 06 '15 at 21:54
  • why not? My code binds the context of the handler to any object you want. – Giuseppe Pes Jun 06 '15 at 21:55
  • That code may be not supported by official documentation, but it works. And your example misses the point of the question - to provide `this` context automatically. – vitaly-t Jun 06 '15 at 21:56
  • I am sorry, but I don't follow you. You 're question is `Is it possible to replace this context when sending an event?`. My solution does exactly this. I don't care if you don't accept the answer, but I would like to understand why it is wrong. – Giuseppe Pes Jun 06 '15 at 21:59
  • I can't even understand your code, function `withContext`, where parameters `context` and `that` come from. – vitaly-t Jun 06 '15 at 22:51
  • note `withContext` returns a function which is used as input in each handler, so data e context are passed in input by the event emiter – Giuseppe Pes Jun 06 '15 at 22:55
  • It is not a solution, it is a work-around. – vitaly-t Jun 07 '15 at 00:48