Skip to content

Instantly share code, notes, and snippets.

@hjdivad
Last active July 11, 2017 20:01
Show Gist options
  • Save hjdivad/7f94a0e81ea38d9022a5007ef8be15c8 to your computer and use it in GitHub Desktop.
Save hjdivad/7f94a0e81ea38d9022a5007ef8be15c8 to your computer and use it in GitHub Desktop.
set -> destroy -> unload semantics
import Ember from 'ember';
import DS from 'ember-data';
const { RSVP: { resolve }} = Ember;
export default DS.Adapter.extend({
deleteRecord() {
return resolve(null);
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
});
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(),
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
author: belongsTo('author', { inverse: null, async: false }),
name: attr(),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let payload = {
data: {
id: 1,
type: 'book',
attributes: {
name: 'History of the Decline and Fall of the Roman Empire',
},
relationships: {
author: {
data: {
id: 2,
type: 'author',
}
}
}
},
included: [{
id: 2,
type: 'author',
attributes: {
name: 'Edward Gibbon'
},
}, {
id: 3,
type: 'author',
attributes: {
name: 'David J. Hamilton',
},
}]
};
let store = this.get('store');
store.push(payload);
return store.peekRecord('book', 1);
},
actions: {
setNewAuthor() {
let store = this.get('store');
let david = store.peekRecord('author', 3);
let book = store.peekRecord('book', 1);
book.set('author', david);
},
destroyNewAuthor() {
let store = this.get('store');
let david = store.peekRecord('author', 3);
david.destroyRecord();
},
unloadNewAuthor() {
let store = this.get('store');
let david = store.peekRecord('author', 3);
david.unloadRecord();
}
},
});
<h1>Welcome to {{appName}}</h1>
book: {{model.name}}
<br>
<br>
author: {{model.author.name}}
<br>
<br>
<button {{action 'setNewAuthor'}}>set</button>
<button {{action 'destroyNewAuthor'}}>destroy</button>
<button {{action 'unloadNewAuthor'}}>unload</button>
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment