Permalink
Browse files

fixed; setting default DocumentArrays

closes #953

Conflicts:

	lib/document.js
	lib/types/documentarray.js
  • Loading branch information...
1 parent 4286fa2 commit 17ff241b678df178efdc190f93e20794a1d6f670 @aheckmann aheckmann committed Jun 8, 2012
Showing with 20 additions and 17 deletions.
  1. +4 −4 lib/document.js
  2. +2 −2 lib/schema/array.js
  3. +11 −8 lib/schema/documentarray.js
  4. +2 −2 lib/schematype.js
  5. +1 −1 lib/types/documentarray.js
View
@@ -148,22 +148,22 @@ Document.prototype.buildDoc = function (fields) {
// apply defaults to all non-excluded fields
if (p in fields) continue;
- def = type.getDefault(self);
+ def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self._activePaths.default(p);
}
} else if (p in fields) {
// selected field
- def = type.getDefault(self);
+ def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self._activePaths.default(p);
}
}
} else {
- def = type.getDefault(self);
+ def = type.getDefault(self, true);
if ('undefined' !== typeof def) {
doc_[piece] = def;
self._activePaths.default(p);
@@ -427,7 +427,7 @@ Document.prototype.set = function (path, val, type) {
} else if (!constructing &&
null != val &&
path in this._activePaths.states.default &&
- deepEqual(val, schema.getDefault(this))) {
+ deepEqual(val, schema.getDefault(this, constructing))) {
// special case:
// a path with a default was $unset on the server
// and the user is setting it to the same value again
View
@@ -27,8 +27,6 @@ var SchemaType = require('../schematype')
*/
function SchemaArray (key, cast, options) {
- SchemaType.call(this, key, options);
-
if (cast) {
var castOptions = {};
@@ -48,6 +46,8 @@ function SchemaArray (key, cast, options) {
this.caster = new caster(null, castOptions);
}
+ SchemaType.call(this, key, options);
+
var self = this
, defaultArr
, fn;
@@ -39,16 +39,19 @@ function DocumentArray (key, schema, options) {
for (var i in schema.statics)
EmbeddedDocument[i] = schema.statics[i];
- ArrayType.call(this, key, EmbeddedDocument, options);
-
- this.caster = EmbeddedDocument;
- this.caster.options = options;
+ EmbeddedDocument.options = options;
+ this.schema = schema;
- var self = this;
+ ArrayType.call(this, key, EmbeddedDocument, options);
this.schema = schema;
+ var path = this.path;
+ var fn = this.defaultValue;
+
this.default(function(){
- return new MongooseDocumentArray([], self.path, this);
+ var arr = fn.call(this);
+ if (!Array.isArray(arr)) arr = [arr];
+ return new MongooseDocumentArray(arr, path, this);
});
};
@@ -111,13 +114,13 @@ DocumentArray.prototype.cast = function (value, doc, init, prev) {
while (i--) {
if (!(value[i] instanceof Subdocument)) {
if (init) {
- subdoc = new this.caster(null, value);
+ subdoc = new this.casterConstructor(null, value);
// skip _id for pre-init hooks
delete subdoc._doc._id;
value[i] = subdoc.init(value[i]);
} else {
subdoc = prev && prev.id(value[i]._id) ||
- new this.caster(null, value);
+ new this.casterConstructor(null, value);
subdoc.set(value[i]);
// if set() is hooked it will have no return value
// see gh-746
View
@@ -215,13 +215,13 @@ SchemaType.prototype.required = function (required) {
* @api private
*/
-SchemaType.prototype.getDefault = function (scope) {
+SchemaType.prototype.getDefault = function (scope, init) {
var ret = 'function' === typeof this.defaultValue
? this.defaultValue.call(scope)
: this.defaultValue;
if (null !== ret && undefined !== ret) {
- return this.cast(ret, scope);
+ return this.cast(ret, scope, init);
} else {
return ret;
}
@@ -52,7 +52,7 @@ MongooseDocumentArray.prototype.__proto__ = MongooseArray.prototype;
*/
MongooseDocumentArray.prototype._cast = function (value) {
- var doc = new this._schema.caster(value, this);
+ var doc = new this._schema.casterConstructor(value, this);
return doc;
};

0 comments on commit 17ff241

Please sign in to comment.