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:
i can't seem export correctly. i'm using this doc.
myclass = exports? , exports or @myclass = {}
doesn't seem work, split files now.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()
Comments
Post a Comment