Skip to content

Commit

Permalink
fixed; subdoc#ownerDocument()
Browse files Browse the repository at this point in the history
closes #1385
  • Loading branch information
aheckmann committed Mar 18, 2013
1 parent 6f49635 commit 5d286ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/types/documentarray.js
Expand Up @@ -58,6 +58,11 @@ MongooseDocumentArray.prototype.__proto__ = MongooseArray.prototype;

MongooseDocumentArray.prototype._cast = function (value) {
if (value instanceof this._schema.casterConstructor) {
if (!(value.__parent && value.__parentArray)) {
// value may have been created using array.create()
value.__parent = this._parent;
value.__parentArray = this;
}
return value;
}

Expand Down
10 changes: 10 additions & 0 deletions test/types.documentarray.test.js
Expand Up @@ -240,6 +240,16 @@ describe('types.documentarray', function(){
})
})
})
it('corrects #ownerDocument() if value was created with array.create() (gh-1385)', function(done){
var mg = new mongoose.Mongoose;
var M = mg.model('1385', { docs: [{ name: String }] });
var m = new M;
var doc = m.docs.create({ name: 'test 1385' });
assert.notEqual(String(doc.ownerDocument()._id), String(m._id));
m.docs.push(doc);
assert.equal(doc.ownerDocument()._id, String(m._id));
done();
})
})

it('#push should work on EmbeddedDocuments more than 2 levels deep', function (done) {
Expand Down

0 comments on commit 5d286ed

Please sign in to comment.