javascript - Node.js server/client module confusion, EventEmitter woes -


i'm trying write library can used in node.js or on client-side.

i'm running two issues:

  1. i can't seem export correctly. i'm using this doc. myclass = exports? , exports or @myclass = {} doesn't seem work, split files now.

  2. the library emits events; i'm hoping can clarify confusion on how more simply. follow me below :)

node.js:

library:

    {eventemitter} = require 'events'      class myclass extends eventemitter        emitsomething: (key, data) ->          @emit key, data  module.exports = myclass 

required:

myclass = require 'myclass' myclass = new myclass()  myclass.on 'someevent', (data) ->   console.log data   # bare not using `emit` directly. data =    key: 'value'  myclass.emitsomething 'someevent', data 

client-side

eventemitter included.

class myclass extends eventemitter    emitsomething: (key, data) ->      @trigger key, [ data ] # that's stupid. 

the library file included in script , somewhere do:

myclass = new myclass()  myclass.on 'someevent', (data) ->   console.log data   data =    key: 'value'  myclass.emitsomething 'someevent', data 

client-side

backbone.js/underscore.js included (not eventemitter library above).

class myclass    constructor: () ->      _.extend @, backbone.events    emitsomething: (key, data) ->      @trigger key, data # notice difference. 

the library file included in script , somewhere do:

myclass = new myclass()  myclass.on 'someevent', (data) ->   console.log data   data =    key: 'value'  myclass.emitsomething 'someevent', data 

so, uh, what's best way write library emits events both node , browser? eventemitter library seemed conflict backbone when had them both included (it needs work on node, , or without backbone). there's got simpler solution!

you should consider using browserify, let use myclass module client side since includes version of nodecore events.

so client side:

myclass = require('./myclass') myclass = new myclass() 

http://browserify.org/

https://github.com/substack/coffeeify


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -