Permalink
Browse files

fix for hooks call - multi-level embedded documents

  • Loading branch information...
1 parent d41da00 commit bcbf90dcb4f66ffb3be48a69e540ac75f0435c87 @gitfy gitfy committed with aheckmann Oct 13, 2011
Showing with 108 additions and 40 deletions.
  1. +38 −2 lib/document.js
  2. +36 −37 lib/model.js
  3. +34 −1 test/model.test.js
View
@@ -743,8 +743,44 @@ Document.prototype._registerHooks = function () {
var self = this;
if (!this.save) return;
-
- this.pre('save', function checkForExistingErrors (next) {
+
+ this.pre('save', function (next) {
+ // we keep the error semaphore to make sure we don't
+ // call `save` unnecessarily (we only need 1 error)
+ var subdocs = 0
+ , DocumentArray = require('./types/documentarray')
+ , error = false
+ , self = this;
+
+ var arrays = this._activePaths
+ .map('init', 'modify', function (i) {
+ return self.getValue(i);
+ })
+ .filter(function (val) {
+ return (val && val instanceof DocumentArray && val.length);
+ });
+
+ if (!arrays.length)
+ return next();
+//require('sys').puts(this.name);
+ arrays.forEach(function (array) {
+ subdocs += array.length;
+ array.forEach(function (value) {
+ if (!error)
+ value.save(function (err) {
+ if (!error) {
+ if (err) {
+ error = true;
+ next(err);
+ } else
+ --subdocs || next();
+ }
+ });
+ });
+ });
+ }, function (err) {
+ this.db.emit('error', err);
+ }).pre('save', function checkForExistingErrors (next) {
if (self._saveError){
next(self._saveError);
self._saveError = null;
View
@@ -5,7 +5,6 @@
var Document = require('./document')
, MongooseArray = require('./types/array')
- , DocumentArray = require('./types/documentarray')
, MongooseBuffer = require('./types/buffer')
, MongooseError = require('./error')
, Query = require('./query')
@@ -458,42 +457,42 @@ Model.prototype.remove = function remove (fn) {
Model.prototype._registerHooks = function registerHooks () {
// make sure to pass along all the errors from subdocuments
- this.pre('save', function (next) {
- // we keep the error semaphore to make sure we don't
- // call `save` unnecessarily (we only need 1 error)
- var subdocs = 0
- , error = false
- , self = this;
-
- var arrays = this._activePaths
- .map('init', 'modify', function (i) {
- return self.getValue(i);
- })
- .filter(function (val) {
- return (val && val instanceof DocumentArray && val.length);
- });
-
- if (!arrays.length)
- return next();
-
- arrays.forEach(function (array) {
- subdocs += array.length;
- array.forEach(function (value) {
- if (!error)
- value.save(function (err) {
- if (!error) {
- if (err) {
- error = true;
- next(err);
- } else
- --subdocs || next();
- }
- });
- });
- });
- }, function (err) {
- this.db.emit('error', err);
- });
+// this.pre('save', function (next) {
+// // we keep the error semaphore to make sure we don't
+// // call `save` unnecessarily (we only need 1 error)
+// var subdocs = 0
+// , error = false
+// , self = this;
+//
+// var arrays = this._activePaths
+// .map('init', 'modify', function (i) {
+// return self.getValue(i);
+// })
+// .filter(function (val) {
+// return (val && val instanceof DocumentArray && val.length);
+// });
+//
+// if (!arrays.length)
+// return next();
+//
+// arrays.forEach(function (array) {
+// subdocs += array.length;
+// array.forEach(function (value) {
+// if (!error)
+// value.save(function (err) {
+// if (!error) {
+// if (err) {
+// error = true;
+// next(err);
+// } else
+// --subdocs || next();
+// }
+// });
+// });
+// });
+// }, function (err) {
+// this.db.emit('error', err);
+// });
Document.prototype._registerHooks.call(this);
};
View
@@ -3144,7 +3144,40 @@ module.exports = {
},
- 'test post hooks': function () {
+ 'pre hooks called on all sub levels': function () {
+ var db = start();
+ var grandSchema = new Schema({ name : String });
+ grandSchema.pre('save', function (next) {
+ this.name = 'grand';
+ next();
+ });
+ var childSchema = new Schema({ name : String, grand : [grandSchema]});
+ childSchema.pre('save', function (next) {
+ this.name = 'child';
+ next();
+ });
+ var schema = new Schema({ name: String, child : [childSchema] });
+
+ schema.pre('save', function (next) {
+ this.name = 'parent';
+ next();
+ });
+
+ var S = db.model('presave_hook', schema, 'presave_hook');
+ var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
+
+ s.save(function (err, doc) {
+ db.close();
+ should.strictEqual(null, err);
+ doc.name.should.eql('parent');
+ doc.name.child[0].name.should.eql('child');
+ doc.name.child[0].grand[0].name.should.eql('grand');
+
+ });
+
+ },
+
+ 'test post hooks': function () {
var schema = new Schema({
title: String
})

0 comments on commit bcbf90d

Please sign in to comment.