Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Last active May 9, 2018 22:57
Show Gist options
  • Save GCheung55/951dbd66552c663492da4b578638f397 to your computer and use it in GitHub Desktop.
Save GCheung55/951dbd66552c663492da4b578638f397 to your computer and use it in GitHub Desktop.
Demonstrate issue with loading/unloading record with 1-1 relationships
import DS from 'ember-data';
import RSVP from 'rsvp';
export default DS.RESTAdapter.extend({
deleteRecord() {
const response = this.handleResponse(
204,
{},
{},
undefined
);
return RSVP.resolve(response);
}
});
import DS from 'ember-data';
import RSVP from 'rsvp';
export default DS.RESTAdapter.extend({
deleteRecord() {
const response = this.handleResponse(
204,
{},
{},
undefined
);
return RSVP.resolve(response);
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
destroy(fooModel) {
const store = this.get('store');
const bar = fooModel.get('bar');
/**
* Without this push to null foo data,
* creating a new bar record with same ID will fail
*/
store.push({
data: {
type: 'bar',
id: bar.get('id'),
attributes: {},
relationships: {
foo: {
data: null
}
}
}
});
bar.destroyRecord().then((resp) => {
console.log(resp);
store.unloadRecord(bar);
});
},
pushRecord(fooModel) {
const store = this.get('store');
const bar = store.createRecord('bar', {id: 1});
fooModel.set('bar', bar);
console.log(store.peekRecord('bar', 1));
}
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
foo: belongsTo('foo', {async: false})
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
bar: belongsTo('bar', {async: false})
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
const store = this.get('store');
store.push({
data: [{
type: 'foo',
id: 1,
attributes: {},
relationships: {
bar: {
data: {id: 1, type: 'bar'}
}
}
}, {
type: 'bar',
id: 1,
attributes: {},
relationships: {
foo: {
data: {id: 1, type: 'foo'}
}
}
}]
});
return store.peekRecord('foo', 1);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
foo.bar.id: {{model.bar.id}} <br>
<button {{action "destroy" model}}>Destroy Bar Record from Foo Model</button>
<button {{action "pushRecord" model}}>Push Bar Record</button>
{
"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