diff --git a/lib/document.js b/lib/document.js index 9c8b4855efc..8f801520b02 100644 --- a/lib/document.js +++ b/lib/document.js @@ -36,6 +36,7 @@ var EventEmitter = require('events').EventEmitter function Document (obj, fields, skipId) { this.$__ = new InternalCache; + this.$__.emitter = new EventEmitter(); this.isNew = true; this.errors = undefined; @@ -54,7 +55,7 @@ function Document (obj, fields, skipId) { this.$__.activePaths.require(required[i]); } - setMaxListeners.call(this, 0); + this.$__.emitter.setMaxListeners(0); this._doc = this.$__buildDoc(obj, fields, skipId); if (obj) { @@ -78,7 +79,34 @@ function Document (obj, fields, skipId) { /*! * Inherit from EventEmitter. */ -Document.prototype = Object.create( EventEmitter.prototype ); +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); +}; + Document.prototype.constructor = Document; /** diff --git a/lib/utils.js b/lib/utils.js index fbea3ba8be5..cffa9a40c67 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -729,5 +729,19 @@ exports.mergeClone = function(to, from) { } } } -} +}; + +/** + * Executes a function on each element of an array (like _.each) + * + * @param {Array} arr + * @param {Function} fn + * @api private + */ + +exports.each = function(arr, fn) { + for (var i = 0; i < arr.length; ++i) { + fn(arr[i]); + } +}; diff --git a/test/types.documentarray.test.js b/test/types.documentarray.test.js index 2609fd7c259..32670a6e15a 100644 --- a/test/types.documentarray.test.js +++ b/test/types.documentarray.test.js @@ -432,12 +432,12 @@ describe('types.documentarray', function(){ M.create({ docs: [{v: 900}] }, function(error, m) { m.shouldPrint = true; assert.ifError(error); - var numListeners = m._events.save.length; + var numListeners = m.listeners('save').length; assert.ok(numListeners > 0); m.docs = [{ v: 9000 }]; m.save(function(error, m) { assert.ifError(error); - assert.equal(numListeners, m._events.save.length); + assert.equal(numListeners, m.listeners('save').length); done(); }); });