Skip to content
This repository
Browse code

misc refactoring

  • Loading branch information...
commit e7087d1f1f56f8624ccd7ff1a8c7de02de4e9b50 1 parent 458bd91
Aaron Heckmann authored October 14, 2011
11  lib/document.js
@@ -14,7 +14,8 @@ var EventEmitter = require('events').EventEmitter
14 14
   , StateMachine = require('./statemachine')
15 15
   , ActiveRoster = StateMachine.ctor('require', 'modify', 'init')
16 16
   , deepEqual = utils.deepEqual
17  
-  , hooks = require('hooks');
  17
+  , hooks = require('hooks')
  18
+  , DocumentArray
18 19
 
19 20
 /**
20 21
  * Document constructor.
@@ -740,20 +741,20 @@ Document.prototype.__defineGetter__('schema', function () {
740 741
  */
741 742
 
742 743
 Document.prototype._registerHooks = function () {
  744
+  if (!this.save) return;
  745
+
743 746
   var self = this;
  747
+  DocumentArray || (DocumentArray = require('./types/documentarray'));
744 748
 
745  
-  if (!this.save) return;
746  
-  
747 749
   this.pre('save', function (next) {
748 750
     // we keep the error semaphore to make sure we don't
749 751
     // call `save` unnecessarily (we only need 1 error)
750 752
     var subdocs = 0
751  
-      , DocumentArray = require('./types/documentarray')
752 753
       , error = false
753 754
       , self = this;
754 755
 
755 756
     var arrays = this._activePaths
756  
-      .map('init', 'modify', function (i) {
  757
+    .map('init', 'modify', function (i) {
757 758
       return self.getValue(i);
758 759
     })
759 760
     .filter(function (val) {
62  test/model.test.js
@@ -3146,21 +3146,24 @@ module.exports = {
3146 3146
 
3147 3147
   'pre hooks called on all sub levels': function () {
3148 3148
     var db = start();
  3149
+
3149 3150
     var grandSchema = new Schema({ name : String });
3150 3151
     grandSchema.pre('save', function (next) {
3151 3152
       this.name = 'grand';
3152  
-        next();
3153  
-      });
  3153
+      next();
  3154
+    });
  3155
+
3154 3156
     var childSchema = new Schema({ name : String, grand : [grandSchema]});
3155 3157
     childSchema.pre('save', function (next) {
3156 3158
       this.name = 'child';
3157  
-        next();
  3159
+      next();
3158 3160
     });
  3161
+
3159 3162
     var schema = new Schema({ name: String, child : [childSchema] });
3160 3163
 
3161 3164
     schema.pre('save', function (next) {
3162 3165
       this.name = 'parent';
3163  
-      next(); 
  3166
+      next();
3164 3167
     });
3165 3168
 
3166 3169
     var S = db.model('presave_hook', schema, 'presave_hook');
@@ -3172,40 +3175,39 @@ module.exports = {
3172 3175
       doc.name.should.eql('parent');
3173 3176
       doc.name.child[0].name.should.eql('child');
3174 3177
       doc.name.child[0].grand[0].name.should.eql('grand');
3175  
-        
3176 3178
     });
3177  
-
3178 3179
   },
3179 3180
 
3180 3181
   'pre hooks error on all sub levels': function () {
3181  
-      var db = start();
3182  
-      var grandSchema = new Schema({ name : String });
3183  
-      grandSchema.pre('save', function (next) {
3184  
-        next(new Error('Error 101'));
3185  
-      });
3186  
-      var childSchema = new Schema({ name : String, grand : [grandSchema]});
3187  
-      childSchema.pre('save', function (next) {
3188  
-        this.name = 'child';
3189  
-        next();
3190  
-      });
3191  
-      var schema = new Schema({ name: String, child : [childSchema] });
  3182
+    var db = start();
3192 3183
 
3193  
-      schema.pre('save', function (next) {
3194  
-        this.name = 'parent';
3195  
-        next(); 
3196  
-      });
  3184
+    var grandSchema = new Schema({ name : String });
  3185
+    grandSchema.pre('save', function (next) {
  3186
+      next(new Error('Error 101'));
  3187
+    });
3197 3188
 
3198  
-      var S = db.model('presave_hook_error', schema, 'presave_hook_error');
3199  
-      var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
  3189
+    var childSchema = new Schema({ name : String, grand : [grandSchema]});
  3190
+    childSchema.pre('save', function (next) {
  3191
+      this.name = 'child';
  3192
+      next();
  3193
+    });
3200 3194
 
3201  
-      s.save(function (err, doc) {
3202  
-        db.close();
3203  
-        err.should.be.an.instanceof(Error);
3204  
-        err.message.should.eql('Error 101');
3205  
-      });
  3195
+    var schema = new Schema({ name: String, child : [childSchema] });
  3196
+    schema.pre('save', function (next) {
  3197
+      this.name = 'parent';
  3198
+      next();
  3199
+    });
  3200
+
  3201
+    var S = db.model('presave_hook_error', schema, 'presave_hook_error');
  3202
+    var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
  3203
+
  3204
+    s.save(function (err, doc) {
  3205
+      db.close();
  3206
+      err.should.be.an.instanceof(Error);
  3207
+      err.message.should.eql('Error 101');
  3208
+    });
  3209
+  },
3206 3210
 
3207  
-    },
3208  
-      
3209 3211
   'test post hooks': function () {
3210 3212
     var schema = new Schema({
3211 3213
             title: String

0 notes on commit e7087d1

Please sign in to comment.
Something went wrong with that request. Please try again.