Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  180] [ 6]  / answers: 1 / hits: 17161  / 12 Years ago, fri, may 18, 2012, 12:00:00

I want to emit events from one file/module/script and listen to them in another file/module/script. How can I share the emitter variable between them without polluting the global namespace?



Thanks!


More From » node.js

 Answers
11

You can pass arguments to require calls thusly:


var myModule = require('myModule')(Events)

And then in "myModule"


module.exports = function(Events) {
// Set up Event listeners here
}

With that said, if you want to share an event emitter, create an emitter object and then pass to your "file/module/script" in a require call.


Update:


Though correct, this is a code smell as you are now tightly coupling the modules together. Instead, consider using a centralized event bus that can be required into each module.


[#85486] Thursday, May 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;