2013-05-19 98 views
1

我試圖「橋接」兩個事件偵聽器,以便通過一個事件觸發它們。而且,在這個「鏈」中,我可以將更多的值作爲參數傳遞給後面的聽衆。使用單個事件橋接兩個事件偵聽器

我試圖通過如下

var events = require('events'); 

var receiv = new events.EventEmitter(); 
var midl = new events.EventEmitter(); 

receiv.on("test", function() { 
    console.log("HERE", arguments); 
}); 

midl.on("test", receiv.on.bind(receiv, "test", "foo", "bar")); 

midl.emit("test", 123); 

然而,這種做法似乎的bug EventEmitter因爲我收到以下錯誤

TypeError: listener must be a function 
    at TypeError (<anonymous>) 
    at EventEmitter.addListener (events.js:130:11) 
    at EventEmitter.emit (events.js:95:17) 
    at Object.<anonymous> (myEventTest.js:12:6) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Function.Module.runMain (module.js:497:10) 
    at startup (node.js:119:16) 

乾杯來實現它。

P.S.我使用節點v0.10.5

回答

2

對不起,我找到了解決辦法。

var events = require('events'); 

var receiv = new events.EventEmitter(); 
var midl = new events.EventEmitter(); 

receiv.on("test", function() { 
    console.log("HERE", arguments); 
}); 

midl.on("test", receiv.emit.bind(receiv, "test", "foo", "bar")); 

midl.emit("test", 123);