Created
August 27, 2018 13:57
-
-
Save mupkoo/1a28c86521340e2dcc528567d04d6136 to your computer and use it in GitHub Desktop.
Cascade delete has many relations Ember Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from 'ember-data/model'; | |
import { belongsTo } from 'ember-data/relationships'; | |
export default Model.extend({ | |
post: belongsTo('post') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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