Skip to content

Commit

Permalink
wip: rework "trigger parent" to use operation hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Jun 3, 2016
1 parent 6028826 commit d0cc791
Showing 1 changed file with 59 additions and 40 deletions.
99 changes: 59 additions & 40 deletions lib/relation-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2429,49 +2429,68 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)

scopeDefinition.related = scopeMethods.related;

return definition;
// Add "after save" and "after delete" observers to persist changes
// on the parent model
modelTo.observe('after save', function(ctx, next) {
var inst = ctx.instance;
if (!inst) {
debug('"after save" hook for an embedded model (EmbedsMany) ' +
'did not provide ctx.instance. Skipping update of the parent model.');
return next();
}

if (typeof inst._getRelationToParent !== 'function') {
debug('The embedded instance was not initialized by ' +
'prepareEmbeddedInstance(). Skipping update of the parent model.');
return next();
}

var relation = inst._getRelationToParent();
var embeddedList = relation.embeddedList();
var propertyName = relation.definition.keyFrom;
var modelInstance = relation.modelInstance;
modelInstance.updateAttribute(propertyName, embeddedList, next);
});

modelTo.observe('after delete', function(ctx, next) {
var inst = ctx.instance;
if (!inst) {
debug('"after delete" hook for an embedded model (EmbedsMany) ' +
'did not provide ctx.instance. Skipping update of the parent model.');
return next();
}

if (typeof inst._getRelationToParent !== 'function') {
debug('The embedded instance was not initialized by ' +
'prepareEmbeddedInstance(). Skipping update of the parent model.');
return next();
}

var relation = inst._getRelationToParent();

var embeddedList = relation.embeddedList();
var index = embeddedList.indexOf(inst);
if (index > -1) embeddedList.splice(index, 1);

var propertyName = relation.definition.keyFrom;
var modelInstance = relation.modelInstance;
modelInstance.updateAttribute(propertyName, embeddedList, next);
});
};

EmbedsMany.prototype.prepareEmbeddedInstance = function(inst) {
if (inst && inst.triggerParent !== 'function') {
var self = this;
var propertyName = this.definition.keyFrom;
var modelInstance = this.modelInstance;
if (this.definition.options.persistent) {
var pk = this.definition.keyTo;
inst.__persisted = !!inst[pk];
} else {
inst.__persisted = true;
}
inst.triggerParent = function(actionName, callback) {
if (actionName === 'save' || actionName === 'destroy') {
var embeddedList = self.embeddedList();
if (actionName === 'destroy') {
var index = embeddedList.indexOf(inst);
if (index > -1) embeddedList.splice(index, 1);
}
modelInstance.updateAttribute(propertyName,
embeddedList, function(err, modelInst) {
callback(err, err ? null : modelInst);
});
} else {
process.nextTick(callback);
}
};
var originalTrigger = inst.trigger;
inst.trigger = function(actionName, work, data, callback) {
if (typeof work === 'function') {
var originalWork = work;
work = function(next) {
originalWork.call(this, function(done) {
inst.triggerParent(actionName, function(err, inst) {
next(done); // TODO [fabien] - error handling?
});
});
};
}
originalTrigger.call(this, actionName, work, data, callback);
};
if (!inst || inst._getRelationToParent === 'function') return;

var self = this;
inst._getRelationToParent = function() {
return self;
}

if (this.definition.options.persistent) {
var pk = this.definition.keyTo;
inst.__persisted = !!inst[pk];
} else {
inst.__persisted = true;
}
};

Expand Down

0 comments on commit d0cc791

Please sign in to comment.