Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active July 2, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runspired/e9ee98ccc89fad2a07d9c86f2541a763 to your computer and use it in GitHub Desktop.
Save runspired/e9ee98ccc89fad2a07d9c86f2541a763 to your computer and use it in GitHub Desktop.
Cascade Delete
import { Promise, defer, resolve } from 'rsvp';
export default class AppAdapter {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
deleteRecord(store, modelClass, snapshot) {
// usually would be this when extending another adapter
// const deletionPromise = this._super(...arguments);
const deletionPromise = resolve();
const hasCascade = snapshot.adapterOptions &&
Array.isArray(snapshot.adapterOptions.cascadeDelete);
let data = null;
return deletionPromise.then(() => {
if (hasCascade) {
const toDelete = snapshot.adapterOptions.cascadeDelete;
const record = snapshot.record;
const clearedRelationships = {};
snapshot.eachRelationship((name, meta) => {
if (toDelete.indexOf(name) !== -1) {
const isMany = meta.kind === 'hasMany';
clearedRelationships[name] = {
data: isMany ? [] : null
};
const related = record[meta.kind](name);
if (isMany) {
const relatedRecords = related.ids().map(id => {
return store.peekRecord(meta.type, id);
});
relatedRecords.map(relatedRecord => pushDeletion(store, relatedRecord));
} else if (related) {
pushDeletion(store, related);
}
}
});
data = {
type: snapshot.modelName,
id: snapshot.id,
relationships: clearedRelationships
};
}
return { data };
});
}
}
function pushDeletion(store, record) {
if (!record) { return; }
if (record.get('isNew')) {
record.unloadRecord();
return;
}
let relationships = {};
let hasRelationships = false;
record.eachRelationship((name, { kind }) => {
hasRelationships = true;
relationships[name] = {
data: kind === 'hasMany' ? [] : null
};
});
if (hasRelationships) {
store.push({
data: {
type: record.constructor.modelName,
id: record.get('id'),
relationships
}
});
}
record.unloadRecord();
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this._super(...arguments);
this.allPeople = this.store.peekAll('person');
this.person = this.store.push({
data: {
type: 'person',
id: '1',
attributes: { name: 'John' },
relationships: {
friends: {
data: [
{ type: 'person', id: '2' },
{ type: 'person', id: '3' }
]
}
}
},
included: [
{
type: 'person',
id: '2',
attributes: { name: 'James' },
},
{
type: 'person',
id: '3',
attributes: { name: 'Chris' },
}
]
});
},
actions: {
deleteRecord(record) {
record.deleteRecord();
record.save({
adapterOptions: { cascadeDelete: ['friends'] }
}).then(() => {
record.unloadRecord();
// release the retained object
this.set('person', null);
});
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr(),
father: belongsTo('person', {
async: false,
inverse: null }),
friends: hasMany('person', {
async: false,
inverse: 'friends'
})
});
export default class MySerializer {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
normalizeResponse(_, __, data) { return data; }
}
<h1>Welcome to {{appName}}, {{person.name}}</h1>
My Id {{person.id}}.<br>My state is: <b>{{person.currentState.stateName}}</b>
<button {{action "deleteRecord" person}}>Delete Me</button>
<br>
Friends
<ul>
{{#each person.friends as |friend|}}
<li>{{friend.name}}</li>
{{/each}}
</ul>
All People
<ul>
{{#each allPeople as |person|}}
<li>{{person.name}}</li>
{{/each}}
</ul>
<br><br>
{{outlet}}
<br>
<br>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment