Skip to content

Instantly share code, notes, and snippets.

@feanor07
Last active February 20, 2019 13:10
Show Gist options
  • Save feanor07/8d6def7995722e9a5d5ef7988b6ba527 to your computer and use it in GitHub Desktop.
Save feanor07/8d6def7995722e9a5d5ef7988b6ba527 to your computer and use it in GitHub Desktop.
relationship removal working
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this.set('parent', this.store.createRecord('parent', {name:'foo'}))
},
actions: {
addNewChild() {
console.log(`@ foo`)
const child = this.store.createRecord('child', {parent: this.parent})
this.set('child', child)
},
unloadLastChild() {
if (this.child) {
this.child.set('parent', null)
this.child.unloadRecord()
this.set('child', null)
}
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
parent: belongsTo('parent')
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
children: hasMany('child'),
name: attr('string')
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{parent.children.length}}
<button onclick={{action "addNewChild"}}>Add</button>
<button onclick={{action "unloadLastChild"}}>Unload last child</button>
<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