简体   繁体   中英

Node.js eventemitter subscriptions on multiple controllers

I'm having a hard time understanding why this is so complicated, so I'm suspecting I'm doing something very wrong. Say I have two controllers that look something like this. They're nearly clones in regards to endpoints, but not in operation -- kind of like interface implementations

(function(someController){
    //some require statements
    var importantEvent = require("../Connectors/SaveEvents");

    someController.init = function(app){
        //app.get, app.post, etc etc etc
        app.post('/saveThat', function(req, res){
            //do some stuff

            importantEvent.Saved({ from: "someController", data: stuff });
        });
    }

    importantEvent.on('Saved', function(data){
        //let this guy know it happened then do some stuff
    })
})(module.exports);

Ok? ok. My event emitter thing that doesn't work and I suspect is very wrong looks like this... (SaveEvents.js)

(function(ImportantEvent){

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

    eventEmitter.call(ImportantEvent);  //??? is this needed?

    ImportantEvent.Saved = function(origin, dataPayload) {
        //maybe do some stuff to the data
        var self = this;
        self.emit('Saved', { from: origin, data: dataPayload });
    };

    util.inherits(OAuthEvents, eventEmitter);

})(module.exports);

when I use grunt, I get a no-go

c:\projects\srsly\node\someController.js:21
        importantEvent.on('Saved', function(data){
                       ^
TypeError: Object #<Object> has no method 'on'

Got an idea why or how to better go about this?

This will work as you want. Keep in mind that module.exports will be cached and when you run require('ImportantEvent') at second time you'll get the previously initialised instance (singleton)

var inherits = require('util').inherits,
    EventEmitter = require('events').EventEmitter;

function ImportantEvent(){}

inherits(ImportantEvent, EventEmitter);

ImportantEvent.prototype.Saved = function(){}

module.exports = new ImportantEvent();

I think that the problem you're experiencing is due to how you are setting up your event emitter. The on function is part of the prototype of the EventEmitter That means you need to create an instance of it, not just invoke the constructor on an object you already have. If you rewrite your ImportantEvent as follows, you should get the on function and possibly the rest of your stuff will start to work:

module.exports = (function(eve){
    var EventEmitter = require('events').EventEmitter;
    var ImportantEvent = new EventEmitter();

    ImportantEvent.Saved = function(origin, dataPayload) {
        this.emit('Saved', {from: origin, data:dataPayload});
    };

    return ImportantEvent;
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM