Permalink
Browse files

only one listener per DocumentArray

  • Loading branch information...
1 parent 63c2dbb commit 419d4534fd7005f30e5711c8542b1fdbc5ca632b @aheckmann aheckmann committed Sep 1, 2011
Showing with 23 additions and 10 deletions.
  1. +23 −2 lib/types/documentarray.js
  2. +0 −8 lib/types/embedded.js
View
@@ -28,9 +28,12 @@ function MongooseDocumentArray (values, path, doc) {
arr._atomics = [];
arr.validators = [];
arr._path = path;
- arr._parent = doc;
- if (doc)
+
+ if (doc) {
+ arr._parent = doc;
arr._schema = doc.schema.path(path);
+ doc.on('save', arr.notify('save'));
+ }
return arr;
};
@@ -106,6 +109,24 @@ MongooseDocumentArray.prototype.inspect = function () {
};
/**
+ * Create fn that notifies all child docs of event.
+ *
+ * @param {String} event
+ * @api private
+ */
+
+MongooseDocumentArray.prototype.notify = function notify (event) {
+ var self = this;
+ return function () {
+ var i = self.length;
+ while (i--) {
+ self[i].isNew = false;
+ self[i].emit(event);
+ }
+ }
+}
+
+/**
* Module exports.
*/
View
@@ -20,14 +20,6 @@ function EmbeddedDocument (obj, parentArr) {
this.parentArray = parentArr;
this.parent = parentArr._parent;
Document.call(this, obj);
-
- if (this.parent) {
- self = this;
- this.parent.on('save', function () {
- self.isNew = false;
- self.emit('save');
- });
- }
};
/**

0 comments on commit 419d453

Please sign in to comment.