I have a little code question for you.

*code:*

var EventEmitter = require('events').EventEmitter

 

function buildEmitters(name) {

  var self = new EventEmitter

  function sayWhat () {
    self.emit('what')
  }

  self.on('what', function () {
    console.log(name + ' says what')
  })


  function addGroup (group) {
    self.on(group, function () {
      console.log(name + ' was called by group, what')
    })
  }

  function callGroup (group) {
    self.emit(group, name)
  }

  self.sayWhat = sayWhat
  self.addGroup = addGroup
  self.callGroup = callGroup

  return self

}



var A = buildEmitters('grep')
    , B = buildEmitters('cat')
    , C = buildEmitters('tar')


A.addGroup('wheel')
B.addGroup('wheel')
C.addGroup('wheel')


A.sayWhat()
B.sayWhat()

C.callGroup('wheel')


which outputs:

grep says what
cat says what
tar was called by group, what

I would like it to say:

grep says what
cat says what
tar was called by group, what
cat was called by group, what
grep was called by group, what

I know I can use the

if (!(this instanceof buildEmitters)) return new buildEmitters()
EventEmitter.call(this)


along with the 

buildEmitters.prototype = new EventEmitter


and in the addGroup method:

buildEmitters.prototype.on(group, callback)

and in the callGroup method:

buildEmitters.prototype.emit(group)


pattern. It works, and it's used everywhere.

*problem:*
But I am attempting to write code without inheritance and prototype calls. 
Zero.
Just clean and simple little objects. 

*question:*
Is it possible to get 'events' to propagate between the objects that *
buildEmitters* creates? Any patterns, hacks or blatantly obvious 
workarounds that I am missing?

Thanks!


-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to