I'm writing a nodejs module and trying bind an event emitter to "scrappy.get". It seems that I can only bind it to "scrappy" .... so "scrappy.get(key).on('complete'...." does not work.
How do I send the event to the child object 'get'?
My NodeJS module:
var util = require('util'),
EventEmitter = require('events').EventEmitter;
function Scrappy(id) {
}
util.inherits(Scrappy, EventEmitter);
Scrappy.prototype.get = function (key) {
var self = this;
self.emit('complete', "here");
**return this;**
}
module.exports = Scrappy;
My NodeJS app code:
var Scrappy = require('./scrappy.js')
var scrappy = new Scrappy("1234");
scrappy.get("name").on('complete', function(data){
console.log("secondary");
});
Result:
scrappy.get("name").on('complete', function(data){
^
TypeError: Cannot call method 'on' of undefined
Edit: Solved. Adding "return this;" solved it for me. Thank you!