Permalink
Browse files

Merge pull request #890 from aheckmann/889

fix sub doc toObject virtuals application
  • Loading branch information...
2 parents f5ae2df + 98f8627 commit 8f76bc90abf93866d49fe35ac0d72f2187778af0 @rauchg rauchg committed May 2, 2012
Showing with 11 additions and 4 deletions.
  1. +4 −4 lib/document.js
  2. +7 −0 test/document.test.js
View
8 lib/document.js
@@ -1133,11 +1133,11 @@ Document.prototype.toObject = function (options) {
var ret = clone(this._doc, options);
if (options.virtuals || options.getters && false !== options.virtuals) {
- applyGetters(this, ret, 'virtuals');
+ applyGetters(this, ret, 'virtuals', options);
}
if (options.getters) {
- applyGetters(this, ret, 'paths');
+ applyGetters(this, ret, 'paths', options);
}
return ret;
@@ -1153,7 +1153,7 @@ Document.prototype.toObject = function (options) {
* @private
*/
-function applyGetters (self, json, type) {
+function applyGetters (self, json, type, options) {
var schema = self.schema
, paths = Object.keys(schema[type])
, i = paths.length
@@ -1171,7 +1171,7 @@ function applyGetters (self, json, type) {
for (var ii = 0; ii < plen; ++ii) {
part = parts[ii];
if (ii === last) {
- branch[part] = self.get(path);
+ branch[part] = clone(self.get(path), options);
} else {
branch = branch[part] || (branch[part] = {});
}
View
7 test/document.test.js
@@ -30,6 +30,9 @@ TestDocument.prototype.__proto__ = Document.prototype;
*/
var em = new Schema({ title: String, body: String });
+em.virtual('works').get(function () {
+ return 'em virtual works'
+});
var schema = TestDocument.prototype.schema = new Schema({
test : String
, oids : [ObjectId]
@@ -216,6 +219,7 @@ module.exports = {
doc.init({
test : 'test'
, oids : []
+ , em : [{ title: 'title' }]
, nested : {
age : 5
, cool : DocumentObjectId.fromString('4c6c2d6240ced95d0e00003c')
@@ -231,6 +235,7 @@ module.exports = {
DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
clone.nested.path.should.eql('5my path');
should.equal(undefined, clone.nested.agePlus2);
+ should.equal(undefined, clone.em[0].works);
clone = doc.toObject({ virtuals: true });
@@ -240,6 +245,7 @@ module.exports = {
DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
clone.nested.path.should.eql('my path');
clone.nested.agePlus2.should.eql(7);
+ clone.em[0].works.should.eql('em virtual works');
clone = doc.toObject({ getters: true });
@@ -249,6 +255,7 @@ module.exports = {
DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
clone.nested.path.should.eql('5my path');
clone.nested.agePlus2.should.eql(7);
+ clone.em[0].works.should.eql('em virtual works');
},
'test hooks system': function(beforeExit){

0 comments on commit 8f76bc9

Please sign in to comment.