Implement embedded.destroy() integration

This commit is contained in:
Fabien Franzen 2014-08-15 15:24:00 +02:00
parent 21801058c9
commit c2f9ee381c
2 changed files with 23 additions and 1 deletions

View File

@ -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);

View File

@ -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 () {