I am developing an app that deals with files.I have a model called file.It has comments model in a has many relationships.Lets say I have a folder F1.F1 contains 3 files and 1 Folder called (F2).F2 has 3 files.Supposing If am deleting F1.The deleteRecord is performed on F1.I can able to unload the model that are in direct relationship with F1.for this I am using cascade-delete mixin.How can I unload the relation ship model that has nested relation ship with F1 such as the files under F2.
model/file.js
export default DS.Model.extend({
name: DS.attr(),
comments:('comment', { cascadeDelete: true }
});
mixins/cascade-delete.js
import Ember from 'ember';
export default Ember.Mixin.create({
deleteRecord(store, type, snapshot) {
let recordsToUnload = [];
// collect all records to unload into recordsToUnload variable
snapshot.record.eachRelationship((name, descriptor) => {
let { options, kind } = descriptor;
let relationshipName = descriptor.key;
if (options.cascadeDelete && kind === 'hasMany') {
let hasManyRecords = snapshot.record.hasMany(relationshipName).value();
if (hasManyRecords !== null) {
hasManyRecordsArray = hasManyRecords.toArray();
}
recordsToUnload = recordsToUnload.concat(hasManyRecords);
}
if (options.cascadeDelete && kind === 'belongsTo') {
let belongsToRecords = snapshot.record.belongsTo(relationshipName).value();
recordsToUnload = recordsToUnload.concat([ belongsToRecords ]);
}
});
return this._super(...arguments).then((response) => {
recordsToUnload.forEach((childRecord) => {
store.unloadRecord(childRecord);
});
return response;
});
}
});