Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
polymorphic example
import DS from 'ember-data';
import Ember from 'ember';
import { belongsTo, hasMany } from "ember-data/relationships";
export default DS.Model.extend({
make: DS.attr('string'),
owner: belongsTo('owner', { polymorphic: true })
});
import DS from 'ember-data';
import attr from "ember-data/attr";
export default DS.Model.extend({
name: attr('string')
});
import Owner from "twiddle/models/owner";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Owner.extend({
cars: hasMany('car', { inverse: 'owner' })
});
import Owner from "twiddle/models/owner";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Owner.extend({
car: belongsTo('car', { inverse: 'owner' }),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
let store = this.store;
store.push({
data: [{
type: 'car',
id: '1',
attributes: {
make: 'toyota'
},
relationships: {
owner: {
data: {
type: 'user',
id: '1'
}
}
}
},{
type: 'car',
id: '2',
attributes: {
make: 'honda'
},
relationships: {
owner: {
data: {
type: 'shop',
id: '1'
}
}
}
}],
included: [{
type: 'user',
id: '1',
attributes: {
name: 'user 1 name'
},
relationships: {
car: {
data: {
type: 'car',
id: '1'
}
}
}
},{
type: 'shop',
id: '1',
attributes: {
name: 'shop 1 name'
},
relationships: {
cars: {
data: [{
type: 'car',
id: '2'
}]
}
}
}]
});
return store.peekRecord('car', 1);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{model.owner}}
{
"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.16.2",
"ember-template-compiler": "release",
"ember-data": "2.18.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment