Skip to content

Commit

Permalink
refactor: use applyHooks for subdocs in node
Browse files Browse the repository at this point in the history
Re: #2754
  • Loading branch information
vkarpov15 committed Jan 2, 2017
1 parent b75c2c9 commit d7a2dc5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var mpath = require('mpath');
* @api private
*/

function Document(obj, fields, skipId, skipHooks) {
function Document(obj, fields, skipId) {
this.$__ = new InternalCache;
this.$__.emitter = new EventEmitter();
this.isNew = true;
Expand Down
1 change: 1 addition & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function DocumentArray(key, schema, options) {
EmbeddedDocument.prototype.$__setSchema(schema);
EmbeddedDocument.schema = schema;
EmbeddedDocument.prototype.constructor = EmbeddedDocument;
EmbeddedDocument.$isArraySubdocument = true;

// apply methods
for (var i in schema.methods) {
Expand Down
16 changes: 14 additions & 2 deletions lib/services/model/applyHooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var Embedded = require('../../types/embedded');
var PromiseProvider = require('../../promise_provider');
var VersionError = require('../../error').VersionError;

Expand All @@ -14,7 +13,6 @@ module.exports = applyHooks;
*/

function applyHooks(model, schema) {
Embedded = Embedded || require('./types/embedded');
var Promise = PromiseProvider.get();

var q = schema && schema.callQueue;
Expand Down Expand Up @@ -59,6 +57,20 @@ function applyHooks(model, schema) {
});
delete toWrap.post;

// 'init' should be synchronous on subdocuments
if (toWrap.init && (model.$isSingleNested || model.$isArraySubdocument)) {
if (toWrap.init.pre) {
toWrap.init.pre.forEach(function(args) {
model.prototype.$pre.apply(model.prototype, args);
});
}
if (toWrap.init.post) {
toWrap.init.post.forEach(function(args) {
model.prototype.$post.apply(model.prototype, args);
});
}
delete toWrap.init;
}
if (toWrap.set) {
// Set hooks also need to be sync re: gh-3479
newName = '$__original_set';
Expand Down
4 changes: 3 additions & 1 deletion lib/types/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ EmbeddedDocument.prototype.populate = function() {

EmbeddedDocument.prototype.save = function(fn) {
var Promise = PromiseProvider.get();
var _this = this;
return new Promise.ES6(function(resolve) {
fn && fn();
resolve();
_this.emit('save', _this);
_this.constructor.emit('save', _this);
});
};

Expand All @@ -122,7 +125,6 @@ function registerRemoveListener(sub) {
var owner = sub.ownerDocument();

function emitRemove() {
console.log('Emitting remove', sub, sub.constructor);
owner.removeListener('save', emitRemove);
owner.removeListener('remove', emitRemove);
sub.emit('remove', sub);
Expand Down
1 change: 0 additions & 1 deletion test/document.hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ describe('document: hooks:', function() {
});

it('post remove hooks on subdocuments work', function(done) {
console.log('------------');
var db = start();
var sub = new Schema({_id: Number});
var called = {pre: 0, post: 0};
Expand Down
19 changes: 5 additions & 14 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3851,27 +3851,18 @@ describe('Model', function() {
title: String
});

var ParentSchema = new Schema({
embeds: [EmbeddedSchema]
});

EmbeddedSchema.post('save', function() {
save = true;
});

// Don't know how to test those on a embedded document.
// EmbeddedSchema.post('init', function () {
// init = true;
// });

// EmbeddedSchema.post('remove', function () {
// remove = true;
// });
var ParentSchema = new Schema({
embeds: [EmbeddedSchema]
});

mongoose.model('Parent', ParentSchema);

var db = start(),
Parent = db.model('Parent');
var db = start();
var Parent = db.model('Parent');

var parent = new Parent();

Expand Down

0 comments on commit d7a2dc5

Please sign in to comment.