Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ember Data - Local Deletion
import Adapter from 'ember-data/adapters/json-api';
import { resolve } from 'rsvp';
export default class ApplicationAdapter extends Adapter {
deleteRecord(store, ModelClass, snapshot) {
if (snapshot.adapterOptions && snapshot.adapterOptions.isLocalDelete === true) {
return resolve({ data: null });
}
return this._super(...arguments);
}
}
import Ember from 'ember';
let id = 123;
export default Ember.Controller.extend({
people: Ember.A(),
actions: {
createPerson(name) {
let store = this.get('store');
let person = store.push({
data: {
type: 'person',
id: `${id++}`,
attributes: { name }
}
});
this.get('people').addObject(person);
this.set('newPersonName', '');
},
deletePerson(person) {
this.get('people').removeObject(person);
person.destroyRecord({
adapterOptions: {
isLocalDelete: true
}
});
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
name: attr()
});
import Serializer from 'ember-data/serializers/json-api';
export default Serializer.extend();
<h3>Deleting Records Locally</h3>
<p>A simple example</p>
<br>
<ul>
{{#each people as |person|}}
<li>
Name: {{person.name}}<br>
ID: {{person.id}}<br>
<button {{action "deletePerson" person}}>Delete Person</button>
</li>
{{else}}
You have no folks!
{{/each}}
</ul>
<br>
<br>
{{input type="text" value=newPersonName}}
<button {{action "createPerson" newPersonName}}>Create Person</button>
<br>
{{outlet}}
<br>
{
"version": "0.13.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
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.