Permalink
Browse files

Getters/setters for nested virtuals now work.

  • Loading branch information...
1 parent 8fce78a commit 87dc3cf6d92f9a0d4b3b9216e5ddca1c9e51f370 @bnoguchi bnoguchi committed Mar 23, 2011
Showing with 11 additions and 5 deletions.
  1. +7 −1 lib/mongoose/schema.js
  2. +4 −4 test/model.test.js
@@ -358,7 +358,13 @@ Schema.prototype.__defineGetter__('indexes', function () {
Schema.prototype.virtual = function (name) {
var virtuals = this.virtuals || (this.virtuals = {})
- return virtuals[name] || (virtuals[name] = this.tree[name] = new VirtualType());
+ var parts = name.split('.');
+ return virtuals[name] = parts.reduce( function (mem, part, i) {
+ mem[part] || (mem[part] = (i === parts.length-1)
+ ? new VirtualType()
+ : {});
+ return mem[part];
+ }, this.tree);
};
/**
View
@@ -2164,10 +2164,10 @@ module.exports = {
person.get('name.first').should.equal('The');
person.get('name.last').should.equal('Situation');
- person.name.full.should.equal('Michael Sorrentino');
- person.name.full = 'The Situation';
- person.name.first.should.equal('The');
- person.name.last.should.equal('Situation');
+ person.name.full.should.equal('The Situation');
+ person.name.full = 'Michael Sorrentino';
+ person.name.first.should.equal('Michael');
+ person.name.last.should.equal('Sorrentino');
db.close();
},

0 comments on commit 87dc3cf

Please sign in to comment.