Merge pull request #222 from fabien/fix/relation-tests
Fixed duplicate code
This commit is contained in:
commit
8743d75999
|
@ -1696,50 +1696,6 @@ EmbedsMany.prototype.embeddedList = function(modelInstance) {
|
||||||
return embeddedList;
|
return embeddedList;
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbedsMany.prototype.prepareEmbeddedInstance = function(inst) {
|
|
||||||
if (inst && inst.triggerParent !== 'function') {
|
|
||||||
var self = this;
|
|
||||||
var relationName = this.definition.name;
|
|
||||||
var modelInstance = this.modelInstance;
|
|
||||||
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(relationName,
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
EmbedsMany.prototype.embeddedList = function(modelInstance) {
|
|
||||||
modelInstance = modelInstance || this.modelInstance;
|
|
||||||
var embeddedList = modelInstance[this.definition.name] || [];
|
|
||||||
embeddedList.forEach(this.prepareEmbeddedInstance.bind(this));
|
|
||||||
return embeddedList;
|
|
||||||
};
|
|
||||||
|
|
||||||
EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) {
|
EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) {
|
||||||
var modelTo = this.definition.modelTo;
|
var modelTo = this.definition.modelTo;
|
||||||
var modelInstance = this.modelInstance;
|
var modelInstance = this.modelInstance;
|
||||||
|
|
Loading…
Reference in New Issue