2012-11-20 18 views
5

Jak dziedziczę metody events.EventEmitter na module implementującym singletowy wzorzec projektowy?NodeJS - Singleton + Wydarzenia

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

var Singleton = {}; 
util.inherits(Singleton, EventEmitter); 

Singleton.createClient = function(options) { 
    this.url = options.url || null; 

    if(this.url === null) { 
     this.emit('error', 'Invalid url'); 
    } else { 
     this.emit('created', true); 
    } 
} 

module.exports = Singleton; 

Wynika to z błędu: TypeError: Object #<Object> has no method 'emit'

Odpowiedz

7

ja nie widzę Singleton w swoim pytaniu. Masz na myśli coś takiego?

var util = require("util") 
    , EventEmitter = process.EventEmitter 
    , instance; 

function Singleton() { 
    EventEmitter.call(this); 
} 

util.inherits(Singleton, EventEmitter); 

module.exports = { 
    // call it getInstance, createClient, whatever you're doing 
    getInstance: function() { 
    return instance || (instance = new Singleton()); 
    } 
}; 

Byłoby być używany jak:

var Singleton = require('./singleton') 
    , a = Singleton.getInstance() 
    , b = Singleton.getInstance(); 

console.log(a === b) // yep, that's true 

a.on('foo', function(x) { console.log('foo', x); }); 

Singleton.getInstance().emit('foo', 'bar'); // prints "foo bar" 
+0

Użyto '' 'Singleton.prototype.createClient''', aby dodać funkcję z mojego przykładu. W app.js '' 'var S = require ('singleton.js'). GetInstance(); S.createClient ({url: 'test'}). On ('connected', function() {}); '' 'Nie działa' 'TypeError: Can not call method 'on' of undefined''' –

+0

nevermind , musiałem "' 'return self''' do łączenia się do pracy: P thx! –

5

udało mi się wyciągnąć to wyłączyć za pomocą poniższego emiterów zdarzeń singleton klasy. arguments.callee._singletonInstance jest preferowanym sposobem prowadzenia singletons w javascript: http://code.google.com/p/jslibs/wiki/JavascriptTips#Singleton_pattern

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

var emitter = function() { 
    if (arguments.callee._singletonInstance) 
     return arguments.callee._singletonInstance; 
    arguments.callee._singletonInstance = this; 
    EventEmitter.call(this); 
}; 

emitter.prototype.__proto__ = EventEmitter.prototype; 

module.exports = new emitter(); 

Następnie można uzyskać dostęp do emitera zdarzeń w dowolnych modułów przy użyciu następującej

Moduł A:

var emitter = require('<path_to_your_emitter>'); 

emitter.emit('myCustomEvent', arg1, arg2, ....) 

MODUŁ B:

var emitter = require('<path_to_your_emitter>'); 

emitter.on('myCustomEvent', function(arg1, arg2, ...) { 
    . . . this will execute when the event is fired in module A 
}); 
1

Żeby było EAS ier, ja stworzyliśmy pakiet npm: central-event

Co trzeba zrobić, to w pierwszym module:

// Say Something 
 
var emitter = require('central-event'); 
 
emitter.emit('talk', 'hello world');

module B

// Say Something 
 
var emitter = require('central-event'); 
 
emitter.on('talk', function(value){ 
 
    console.log(value); 
 
    // This will pring hello world 
 
});