Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Push Polymorphic
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Hat from "./hat";
export default Hat.extend({
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo } from "ember-data/relationships";
export default Model.extend({
type: attr('string'),
person: belongsTo('person',{async: false, inverse: 'hats'})
});
import Model from "ember-data/model";
import { hasMany } from "ember-data/relationships";
export default Model.extend({
hats: hasMany('hat', { async: false, polymorphic: true }),
});
import Hat from "./hat";
export default Hat.extend({
});
import Ember from 'ember';
import DS from 'ember-data';
const { $ } = Ember;
const personData = {
data: {
id: 1,
type: 'person',
attributes: {}
}
};
const hatData = {
data: {
relationships: {
person: {
data: { id:1, type: 'person' }
}
}
}
};
const bigHatData1 = $.extend(true, {}, hatData, {data: {id:1, type:'big-hat'}});
const bigHatData2 = $.extend(true, {}, hatData, {data: {id:2, type:'big-hat'}});
const smallHatData1 = $.extend(true, {}, hatData, {data: {id:1, type:'small-hat'}});
const smallHatData2 = $.extend(true, {}, hatData, {data: {id:2, type:'small-hat'}});
const smallHatData3 = $.extend(true, {}, hatData, {data: {id:3, type:'small-hat'}});
export default Ember.Route.extend({
model() {
let person = this.store.push(personData);
// works with created .. but not pushed records
// if you even create ONE new record it all works fine
// this.store.createRecord('big-hat', {person});
// this.store.createRecord('small-hat', {person});
this.store.push(bigHatData1);
this.store.push(smallHatData1);
this.store.push(bigHatData2);
this.store.push(smallHatData2);
return person;
},
setupController(controller, person) {
controller.setProperties({
person,
pushedHats: person.get('hats').toArray(),
// in twiddle.json switch to ember-data: "2.13" to see it working
DS
});
}
});
<h4>pushing polymophic records bug ember-data 2.14</h4>
<div>Ember Data: {{DS.VERSION}}</div>
<div>Person: {{person}}</div>
<h4>Should be 4 hats</h4>
<div>{{pushedHats}}</div>
<br>
{
"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",
"mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.14.7"
}
}
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.