2012-11-20 23 views
5

Làm cách nào để kế thừa các sự kiện.EventEmitter phương pháp trên mô-đun triển khai mẫu thiết kế đơn lẻ?NodeJS - Singleton + Sự kiện

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; 

Điều này dẫn đến các lỗi: TypeError: Object #<Object> has no method 'emit'

Trả lời

7

Tôi không thấy mô hình singleton trong câu hỏi của bạn. Bạn có nghĩa là một cái gì đó như thế này?

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()); 
    } 
}; 

Nó sẽ được sử dụng như:

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

Được sử dụng '' 'Singleton.prototype.createClient''' để thêm hàm từ ví dụ của tôi. Trong app.js '' 'var S = require ('singleton.js'). GetInstance(); S.createClient ({url: 'test'}). On ('connected', function() {}); '' 'Không hoạt động' '' TypeError: Không thể gọi phương thức 'on' của undefined''' –

+0

nevermind , đã phải '' 'trở lại tự'' cho chuỗi để làm việc: P thx! –

5

tôi quản lý để kéo giảm giá này bằng cách sử dụng sự kiện singleton sau phát lớp. arguments.callee._singletonInstance là cách ưa thích làm độc thân trong 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(); 

Sau đó bạn có thể truy cập vào phát sự kiện trong bất kỳ các module của bạn bằng cách sử dụng

PHẦN sau A:

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

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

MODULE 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

Để làm cho nó dễ dàng IER, tôi đã tạo ra một gói NPM: central-event

Những gì bạn phải làm là trong các mô-đun đầu tiên:

// 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 
 
});

Các vấn đề liên quan