我正在开发一个处理文件的应用程序。我有一个名为file的模型。它有一个有很多关系的注释模型。让我说我有一个文件夹F1.F1包含3个文件和1个文件夹叫(F2)。 F2有3个文件。假设如果要删除F1.DirectRecord在F1.上执行。我可以卸载与F1直接关系的模型。因此我使用cascade-delete mixin。如何卸载关系船与F1嵌套关系的模型,如F2下的文件。
模型/ file.js
export default DS.Model.extend({
name: DS.attr(),
comments:('comment', { cascadeDelete: true }
});
混入/级联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;
});
}
});