Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cascade delete has many relations Ember Data
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
deleteRecord(store, type, snapshot) {
let recordsForUnloading = [];
snapshot.eachRelationship((name, descriptor) => {
let { options, kind, key } = descriptor;
if (options.onDelete === 'unload' && kind === 'hasMany') {
let records = snapshot.record.hasMany(key).value();
if (records) {
recordsForUnloading = recordsForUnloading.concat(records.toArray());
}
}
});
return this._super(...arguments).then((response) => {
recordsForUnloading.forEach((record) => {
record.unloadRecord();
});
return response;
});
}
});
import Model from 'ember-data/model';
import { belongsTo } from 'ember-data/relationships';
export default Model.extend({
post: belongsTo('post')
});
import Model from 'ember-data/model';
import { hasMany } from 'ember-data/relationships';
export default Model.extend({
comments: hasMany('comment', { onDelete: 'unload' })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.