/
document.js
113 lines (91 loc) · 1.95 KB
/
document.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Module dependencies.
*/
var Document = require('../document')
, inspect = require('util').inspect;
/**
* EmbeddedDocument constructor.
*
* @param {Object} object from db
* @param {MongooseDocumentArray} parent array
* @api private
*/
function EmbeddedDocument (obj, parentArr) {
this.parentArray = parentArr;
this.parent = parentArr.parent;
Document.call(this, obj);
};
/**
* Inherit from Document
*
*/
EmbeddedDocument.prototype.__proto__ = Document.prototype;
/**
* Override set to mark the parent as modified
*
* @api public
*/
var oldSet = Document.prototype.set;
EmbeddedDocument.prototype.set = function (path) {
this.markModified(path);
return oldSet.apply(this, arguments);
};
/**
* Marks parent array as modified
*
* @api private
*/
EmbeddedDocument.prototype.commit =
EmbeddedDocument.prototype.markModified = function (path) {
this._activePaths.modify(path);
if (this.isNew) {
// Mark the WHOLE parent array as modified
// if this is a new document (i.e., we are initializing
// a document),
this.parentArray._markModified();
} else
this.parentArray._markModified(this, path);
};
/**
* Save the subdocument
*
* @api public
*/
EmbeddedDocument.prototype.save = function(fn) {
if (fn)
fn(null);
this.emit('save', this);
return this;
};
/**
* Remove the subdocument
*
* @api public
*/
EmbeddedDocument.prototype.remove = function (fn) {
var _id;
if (!this.willRemove) {
_id = this._doc._id;
if (!_id) {
throw new Error('For your own good, Mongoose does not know ' +
'how to remove an EmbeddedDocument that has no _id');
}
this.parentArray.$pull({ _id: _id });
this.willRemove = true;
}
if (fn)
fn(null);
return this;
};
/**
* Helper for console.log
*
* @api public
*/
EmbeddedDocument.prototype.inspect = function () {
return inspect(this.toObject());
};
/**
* Module exxports.
*/
module.exports = EmbeddedDocument;