From c2f9ee381cc7b470e2cf230a4e00523f11d638b3 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Fri, 15 Aug 2014 15:24:00 +0200 Subject: [PATCH] Implement embedded.destroy() integration --- lib/relation-definition.js | 7 ++++++- test/relations.test.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 2e5f3766..65843c29 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1609,11 +1609,16 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params) 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 = modelInstance[relationName] || []; + 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); diff --git a/test/relations.test.js b/test/relations.test.js index 7f239f33..53f04229 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1805,6 +1805,23 @@ describe('relations', function () { }); }); + it('should remove items from scope - and save parent', function(done) { + Category.findById(category.id, function(err, cat) { + cat.items.at(0).destroy(function(err, link) { + cat.links.should.eql([]); + done(); + }); + }); + }); + + it('should find items on scope - verify destroy', function(done) { + Category.findById(category.id, function(err, cat) { + cat.name.should.equal('Category B'); + cat.links.should.eql([]); + done(); + }); + }); + }); describe('embedsMany - polymorphic relations', function () {