* DocumentArray constructor * * @param {Array} values * @param {String} path the path to this array * @param {Document} doc parent document * @api private * @return {MongooseDocumentArray} * @inherits MongooseArray * @see http://bit.ly/f6CnZU
(values, path, doc)
| 21 | */ |
| 22 | |
| 23 | function MongooseDocumentArray(values, path, doc) { |
| 24 | var arr = [].concat(values); |
| 25 | arr._path = path; |
| 26 | |
| 27 | var props = { |
| 28 | isMongooseArray: true, |
| 29 | isMongooseDocumentArray: true, |
| 30 | validators: [], |
| 31 | _atomics: {}, |
| 32 | _schema: void 0, |
| 33 | _handlers: void 0 |
| 34 | }; |
| 35 | |
| 36 | // Values always have to be passed to the constructor to initialize, since |
| 37 | // otherwise MongooseArray#push will mark the array as modified to the parent. |
| 38 | var keysMA = Object.keys(MongooseArray.mixin); |
| 39 | var numKeys = keysMA.length; |
| 40 | for (var j = 0; j < numKeys; ++j) { |
| 41 | arr[keysMA[j]] = MongooseArray.mixin[keysMA[j]]; |
| 42 | } |
| 43 | |
| 44 | var keysMDA = Object.keys(MongooseDocumentArray.mixin); |
| 45 | numKeys = keysMDA.length; |
| 46 | for (var i = 0; i < numKeys; ++i) { |
| 47 | arr[keysMDA[i]] = MongooseDocumentArray.mixin[keysMDA[i]]; |
| 48 | } |
| 49 | |
| 50 | var keysP = Object.keys(props); |
| 51 | numKeys = keysP.length; |
| 52 | for (var k = 0; k < numKeys; ++k) { |
| 53 | arr[keysP[k]] = props[keysP[k]]; |
| 54 | } |
| 55 | |
| 56 | // Because doc comes from the context of another function, doc === global |
| 57 | // can happen if there was a null somewhere up the chain (see #3020 && #3034) |
| 58 | // RB Jun 17, 2015 updated to check for presence of expected paths instead |
| 59 | // to make more proof against unusual node environments |
| 60 | if (doc && doc instanceof Document) { |
| 61 | arr._parent = doc; |
| 62 | arr._schema = doc.schema.path(path); |
| 63 | arr._handlers = { |
| 64 | isNew: arr.notify('isNew'), |
| 65 | save: arr.notify('save') |
| 66 | }; |
| 67 | |
| 68 | doc.on('save', arr._handlers.save); |
| 69 | doc.on('isNew', arr._handlers.isNew); |
| 70 | } |
| 71 | |
| 72 | return arr; |
| 73 | } |
| 74 | |
| 75 | /*! |
| 76 | * Inherits from MongooseArray |
nothing calls this directly
no outgoing calls
no test coverage detected