Skip to content
This repository
Browse code

Getters/setters for nested virtuals now work.

  • Loading branch information...
commit 87dc3cf6d92f9a0d4b3b9216e5ddca1c9e51f370 1 parent 8fce78a
Brian Noguchi bnoguchi authored

Showing 2 changed files with 11 additions and 5 deletions. Show diff stats Hide diff stats

  1. +7 1 lib/mongoose/schema.js
  2. +4 4 test/model.test.js
8 lib/mongoose/schema.js
@@ -358,7 +358,13 @@ Schema.prototype.__defineGetter__('indexes', function () {
358 358
359 359 Schema.prototype.virtual = function (name) {
360 360 var virtuals = this.virtuals || (this.virtuals = {})
361   - return virtuals[name] || (virtuals[name] = this.tree[name] = new VirtualType());
  361 + var parts = name.split('.');
  362 + return virtuals[name] = parts.reduce( function (mem, part, i) {
  363 + mem[part] || (mem[part] = (i === parts.length-1)
  364 + ? new VirtualType()
  365 + : {});
  366 + return mem[part];
  367 + }, this.tree);
362 368 };
363 369
364 370 /**
8 test/model.test.js
@@ -2164,10 +2164,10 @@ module.exports = {
2164 2164 person.get('name.first').should.equal('The');
2165 2165 person.get('name.last').should.equal('Situation');
2166 2166
2167   - person.name.full.should.equal('Michael Sorrentino');
2168   - person.name.full = 'The Situation';
2169   - person.name.first.should.equal('The');
2170   - person.name.last.should.equal('Situation');
  2167 + person.name.full.should.equal('The Situation');
  2168 + person.name.full = 'Michael Sorrentino';
  2169 + person.name.first.should.equal('Michael');
  2170 + person.name.last.should.equal('Sorrentino');
2171 2171
2172 2172 db.close();
2173 2173 },

0 comments on commit 87dc3cf

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