Implement embedded.destroy() integration
This commit is contained in:
parent
21801058c9
commit
c2f9ee381c
|
@ -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);
|
||||
|
|
|
@ -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 () {
|
||||
|
|
Loading…
Reference in New Issue