Permalink
Browse files

ignore undefined virtual setter return vals

  • Loading branch information...
1 parent 0617c6f commit cf8fe139cbb69908f7f8ef84aaa029df2ed78179 @aheckmann aheckmann committed Aug 23, 2012
Showing with 27 additions and 1 deletion.
  1. +2 −1 lib/virtualtype.js
  2. +25 −0 test/model.test.js
View
@@ -66,7 +66,8 @@ VirtualType.prototype.applyGetters = function (value, scope) {
VirtualType.prototype.applySetters = function (value, scope) {
var v = value;
for (var l = this.setters.length - 1; l >= 0; l--) {
- v = this.setters[l].call(scope, v);
+ var val = this.setters[l].call(scope, v);
+ if (undefined !== val) v = val;
}
return v;
};
View
@@ -1534,6 +1534,31 @@ describe('model', function(){
post.set('titleWithAuthor', 'Huckleberry Finn by Mark Twain')
assert.equal(post.get('title'),'Huckleberry Finn');
assert.equal(post.get('author'),'Mark Twain');
+
+ // return values
+ var schema = new Schema({ name: String, name2: String });
+ var virtualName = schema.virtual('virtualname');
+ virtualName.set(function (v) {
+ this.name = v.substring(11);
+ }).set(function (v) {
+ return v.toUpperCase();
+ });
+
+ var virtualName2 = schema.virtual('virtualname2');
+ virtualName2.set(function (v) {
+ this.name2 = v.substring(11);
+ }).set(function (v) {
+ // undefined return values are ignored
+ v.toUpperCase();
+ });
+
+ var M = db.model('virtual-set', schema);
+ var m = new M;
+ m.virtualname = "my name is hal"
+ m.virtualname2 = "my name is hal";
+
+ assert.equal('HAL', m.name);
+ assert.equal('hal', m.name2);
});
it('should not be saved to the db', function(done){

0 comments on commit cf8fe13

Please sign in to comment.