Permalink
Browse files

misc refactoring

  • Loading branch information...
aheckmann committed Oct 14, 2011
1 parent 458bd91 commit e7087d1f1f56f8624ccd7ff1a8c7de02de4e9b50
Showing with 38 additions and 35 deletions.
  1. +6 −5 lib/document.js
  2. +32 −30 test/model.test.js
View
@@ -14,7 +14,8 @@ var EventEmitter = require('events').EventEmitter
, StateMachine = require('./statemachine')
, ActiveRoster = StateMachine.ctor('require', 'modify', 'init')
, deepEqual = utils.deepEqual
- , hooks = require('hooks');
+ , hooks = require('hooks')
+ , DocumentArray
/**
* Document constructor.
@@ -740,20 +741,20 @@ Document.prototype.__defineGetter__('schema', function () {
*/
Document.prototype._registerHooks = function () {
+ if (!this.save) return;
+
var self = this;
+ DocumentArray || (DocumentArray = require('./types/documentarray'));
- if (!this.save) return;
-
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) {
+ .map('init', 'modify', function (i) {
return self.getValue(i);
})
.filter(function (val) {
View
@@ -3146,21 +3146,24 @@ module.exports = {
'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();
- });
+ next();
+ });
+
var childSchema = new Schema({ name : String, grand : [grandSchema]});
childSchema.pre('save', function (next) {
this.name = 'child';
- next();
+ next();
});
+
var schema = new Schema({ name: String, child : [childSchema] });
schema.pre('save', function (next) {
this.name = 'parent';
- next();
+ next();
});
var S = db.model('presave_hook', schema, 'presave_hook');
@@ -3172,40 +3175,39 @@ module.exports = {
doc.name.should.eql('parent');
doc.name.child[0].name.should.eql('child');
doc.name.child[0].grand[0].name.should.eql('grand');
-
});
-
},
'pre hooks error on all sub levels': function () {
- var db = start();
- var grandSchema = new Schema({ name : String });
- grandSchema.pre('save', function (next) {
- next(new Error('Error 101'));
- });
- 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] });
+ var db = start();
- schema.pre('save', function (next) {
- this.name = 'parent';
- next();
- });
+ var grandSchema = new Schema({ name : String });
+ grandSchema.pre('save', function (next) {
+ next(new Error('Error 101'));
+ });
- var S = db.model('presave_hook_error', schema, 'presave_hook_error');
- var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
+ var childSchema = new Schema({ name : String, grand : [grandSchema]});
+ childSchema.pre('save', function (next) {
+ this.name = 'child';
+ next();
+ });
- s.save(function (err, doc) {
- db.close();
- err.should.be.an.instanceof(Error);
- err.message.should.eql('Error 101');
- });
+ var schema = new Schema({ name: String, child : [childSchema] });
+ schema.pre('save', function (next) {
+ this.name = 'parent';
+ next();
+ });
+
+ var S = db.model('presave_hook_error', schema, 'presave_hook_error');
+ var s = new S({ name : 'a' , child : [ { name : 'b', grand : [{ name : 'c'}] } ]});
+
+ s.save(function (err, doc) {
+ db.close();
+ err.should.be.an.instanceof(Error);
+ err.message.should.eql('Error 101');
+ });
+ },
- },
-
'test post hooks': function () {
var schema = new Schema({
title: String

0 comments on commit e7087d1

Please sign in to comment.