diff --git a/lib/browserDocument.js b/lib/browserDocument.js index bc0ab74b91f..3b04b460336 100644 --- a/lib/browserDocument.js +++ b/lib/browserDocument.js @@ -63,6 +63,7 @@ function Document (obj, schema, fields, skipId, skipInit) { this.$__setSchema(schema); this.$__ = new InternalCache; + this.$__.emitter = new EventEmitter(); this.isNew = true; this.errors = undefined; @@ -101,9 +102,9 @@ function Document (obj, schema, fields, skipId, skipInit) { } /*! - * Inherit from EventEmitter. + * Inherit from the NodeJS document */ -Document.prototype = Object.create( NodeJSDocument.prototype ); +Document.prototype = Object.create(NodeJSDocument.prototype); Document.prototype.constructor = Document; diff --git a/lib/document.js b/lib/document.js index 8f801520b02..bdbede002c9 100644 --- a/lib/document.js +++ b/lib/document.js @@ -77,35 +77,17 @@ function Document (obj, fields, skipId) { } /*! - * Inherit from EventEmitter. + * Document exposes the NodeJS event emitter API, so you can use + * `on`, `once`, etc. */ -Document.prototype.on = function() { - return this.$__.emitter.on.apply(this.$__.emitter, arguments); -}; - -Document.prototype.once = function() { - return this.$__.emitter.once.apply(this.$__.emitter, arguments); -}; - -Document.prototype.emit = function() { - return this.$__.emitter.emit.apply(this.$__.emitter, arguments); -}; - -Document.prototype.listeners = function() { - return this.$__.emitter.listeners.apply(this.$__.emitter, arguments); -}; - -Document.prototype.removeListener = function() { - return this.$__.emitter.removeListener.apply(this.$__.emitter, arguments); -}; - -Document.prototype.setMaxListeners = function() { - return this.$__.emitter.setMaxListeners.apply(this.$__.emitter, arguments); -}; - -Document.prototype.removeAllListeners = function() { - return this.$__.emitter.removeAllListeners.apply(this.$__.emitter, arguments); -}; +utils.each( + ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners', + 'removeAllListeners'], + function(emitterFn) { + Document.prototype[emitterFn] = function() { + return this.$__.emitter[emitterFn].apply(this.$__.emitter, arguments); + }; + }); Document.prototype.constructor = Document;