Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #890 from aheckmann/889

fix sub doc toObject virtuals application
  • Loading branch information...
commit 8f76bc90abf93866d49fe35ac0d72f2187778af0 2 parents f5ae2df + 98f8627
Guillermo Rauch authored May 02, 2012
8  lib/document.js
@@ -1133,11 +1133,11 @@ Document.prototype.toObject = function (options) {
1133 1133
   var ret = clone(this._doc, options);
1134 1134
 
1135 1135
   if (options.virtuals || options.getters && false !== options.virtuals) {
1136  
-    applyGetters(this, ret, 'virtuals');
  1136
+    applyGetters(this, ret, 'virtuals', options);
1137 1137
   }
1138 1138
 
1139 1139
   if (options.getters) {
1140  
-    applyGetters(this, ret, 'paths');
  1140
+    applyGetters(this, ret, 'paths', options);
1141 1141
   }
1142 1142
 
1143 1143
   return ret;
@@ -1153,7 +1153,7 @@ Document.prototype.toObject = function (options) {
1153 1153
  * @private
1154 1154
  */
1155 1155
 
1156  
-function applyGetters (self, json, type) {
  1156
+function applyGetters (self, json, type, options) {
1157 1157
   var schema = self.schema
1158 1158
     , paths = Object.keys(schema[type])
1159 1159
     , i = paths.length
@@ -1171,7 +1171,7 @@ function applyGetters (self, json, type) {
1171 1171
     for (var ii = 0; ii < plen; ++ii) {
1172 1172
       part = parts[ii];
1173 1173
       if (ii === last) {
1174  
-        branch[part] = self.get(path);
  1174
+        branch[part] = clone(self.get(path), options);
1175 1175
       } else {
1176 1176
         branch = branch[part] || (branch[part] = {});
1177 1177
       }
7  test/document.test.js
@@ -30,6 +30,9 @@ TestDocument.prototype.__proto__ = Document.prototype;
30 30
  */
31 31
 
32 32
 var em = new Schema({ title: String, body: String });
  33
+em.virtual('works').get(function () {
  34
+  return 'em virtual works'
  35
+});
33 36
 var schema = TestDocument.prototype.schema = new Schema({
34 37
     test    : String
35 38
   , oids    : [ObjectId]
@@ -216,6 +219,7 @@ module.exports = {
216 219
     doc.init({
217 220
         test    : 'test'
218 221
       , oids    : []
  222
+      , em      : [{ title: 'title' }]
219 223
       , nested  : {
220 224
             age   : 5
221 225
           , cool  : DocumentObjectId.fromString('4c6c2d6240ced95d0e00003c')
@@ -231,6 +235,7 @@ module.exports = {
231 235
     DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
232 236
     clone.nested.path.should.eql('5my path');
233 237
     should.equal(undefined, clone.nested.agePlus2);
  238
+    should.equal(undefined, clone.em[0].works);
234 239
 
235 240
     clone = doc.toObject({ virtuals: true });
236 241
 
@@ -240,6 +245,7 @@ module.exports = {
240 245
     DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
241 246
     clone.nested.path.should.eql('my path');
242 247
     clone.nested.agePlus2.should.eql(7);
  248
+    clone.em[0].works.should.eql('em virtual works');
243 249
 
244 250
     clone = doc.toObject({ getters: true });
245 251
 
@@ -249,6 +255,7 @@ module.exports = {
249 255
     DocumentObjectId.toString(clone.nested.cool).should.eql('4c6c2d6240ced95d0e00003c');
250 256
     clone.nested.path.should.eql('5my path');
251 257
     clone.nested.agePlus2.should.eql(7);
  258
+    clone.em[0].works.should.eql('em virtual works');
252 259
   },
253 260
 
254 261
   'test hooks system': function(beforeExit){

0 notes on commit 8f76bc9

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