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,
inverse: 'person'
}),
});
import Hat from "./hat";
export default Hat.extend({
});
import Ember from 'ember';
import DS from 'ember-data';
const { copy } = Ember;
const personData = {
data: {
id: '1',
type: 'person',
attributes: {}
}
};
const hatData = {
data: {
relationships: {
person: {
data: { id: '1' , type: 'person' }
}
}
}
};
let id = 1;
function makeHat(type) {
const resource = copy(hatData, true);
resource.data.id = `${id++}`;
resource.data.type = type;
return resource;
}
const bigHatData1 = makeHat('big-hat');
const bigHatData2 = makeHat('big-hat');
const smallHatData1 = makeHat('small-hat');
const smallHatData2 = makeHat('small-hat');
const smallHatData3 = makeHat('small-hat');
export default Ember.Route.extend({
model() {
const store = this.get('store');
let person = 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});
store.push(bigHatData1);
store.push(smallHatData1);
store.push(bigHatData2);
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.