Browse files

fixed; can now change populated arrays and save

closes #477
  • Loading branch information...
1 parent 35a2bcd commit ddd3d3211bf3562d5cb4010e2de12a29f11a03df @aheckmann aheckmann committed Aug 28, 2011
Showing with 22 additions and 13 deletions.
  1. +5 −4 lib/document.js
  2. +8 −7 lib/model.js
  3. +8 −1 lib/types/array.js
  4. +1 −1 lib/utils.js
View
9 lib/document.js
@@ -754,9 +754,10 @@ Document.prototype.doQueue = function () {
* @api public
*/
-Document.prototype.toObject = function (ref) {
- if (ref) return this._doc;
- return clone(this._doc, { minimize: true });
+Document.prototype.toObject = function (options) {
+ options || (options = {});
+ options.minimize = true;
+ return clone(this._doc, options);
};
/**
@@ -777,7 +778,7 @@ Document.prototype.toJSON = function () {
*/
Document.prototype.inspect = function () {
- return inspect(this.toObject(true));
+ return inspect(this.toObject());
};
/**
View
15 lib/model.js
@@ -155,14 +155,15 @@ Model.prototype.init = function (doc, query, fn) {
query = null;
}
- var populate = this.getPopulationKeys(query)
- , self = this;
+ var populate = this.getPopulationKeys(query);
if (!populate) {
- // if no population from other models is necessary
return Document.prototype.init.call(this, doc, fn);
}
+ // population from other models is necessary
+ var self = this;
+
function error (err) {
if (fn.run) return;
fn.run = true;
@@ -264,7 +265,7 @@ Model.prototype.save = function (fn) {
if (this.isNew) {
// send entire doc
- this.collection.insert(this.toObject(), options, complete);
+ this.collection.insert(this.toObject({ depopulate: 1 }), options, complete);
this.isNew = false;
// clear atomics
@@ -362,11 +363,11 @@ Model.prototype._delta = function () {
}
}
obj[data.path] = val.toObject
- ? val.toObject() // If the value is an array
+ ? val.toObject({ depopulate: 1 }) // MongooseArray
: Array.isArray(val)
? val.map(function (mem) {
return mem.toObject
- ? mem.toObject()
+ ? mem.toObject({ depopulate: 1 })
: mem.valueOf
? mem.valueOf()
: mem;
@@ -380,7 +381,7 @@ Model.prototype._delta = function () {
// normalize MongooseArray or MongooseNumber
if (type instanceof MongooseArray ||
type instanceof MongooseBuffer) {
- type = type.toObject();
+ type = type.toObject({ depopulate: 1 });
} else if (type._path)
type = type.valueOf();
View
9 lib/types/array.js
@@ -4,6 +4,7 @@
*/
var EmbeddedDocument = require('./document');
+var Document = require('../document');
var ObjectId = require('./objectid');
/**
@@ -299,7 +300,13 @@ MongooseArray.prototype.splice = function () {
* @api public
*/
-MongooseArray.prototype.toObject = function () {
+MongooseArray.prototype.toObject = function (options) {
+ if (options && options.depopulate && this[0] instanceof Document) {
+ return this.map(function (doc) {
+ return doc.toObject(options);
+ });
+ }
+
return this.map(function (doc) {
return doc;
});
View
2 lib/utils.js
@@ -210,7 +210,7 @@ var clone = exports.clone = function clone (obj, options) {
return cloneArray(obj, options);
if (obj.toObject)
- return obj.toObject();
+ return obj.toObject(options);
if (obj.constructor == Object)
return cloneObject(obj, options);

0 comments on commit ddd3d32

Please sign in to comment.