Skip to content

Instantly share code, notes, and snippets.

@pangratz
Last active January 26, 2017 21:52
Show Gist options
  • Save pangratz/aafb8367444dc7a861a9 to your computer and use it in GitHub Desktop.
Save pangratz/aafb8367444dc7a861a9 to your computer and use it in GitHub Desktop.
Polymorphic Records
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
shouldReloadAll: function() {
return true;
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle'
});
<h1>My Library</h1>
<hr>
{{outlet}}
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.get('store').findRecord('my-model', 1);
}
});
<section>
Number of items: {{model.items.length}}
<ul>
{{#each model.items as |item|}}
<li>{{item.title}}</li>
{{/each}}
</ul>
</section>
import Ember from 'ember';
export function initialize() {
$.mockjax({
type: 'GET',
url: '/myModels/1',
status: '200',
dataType: 'json',
responseText: {
myModel: {
id: 1,
items: [
{ id: 1, type: 'lecture' },
{ id: 2, type: 'assignment' }
]
}
}
});
$.mockjax({
type: 'GET',
url: '/lectures/1',
status: '200',
dataType: 'json',
responseText: {
lecture: { id: 1, title: "my first lecture" }
}
});
$.mockjax({
type: 'GET',
url: '/assignments/2',
status: '200',
dataType: 'json',
responseText: {
assignment: { id: 2, title: "my first assignment" }
}
});
}
export default {
name: 'mockjax',
initialize: initialize
};
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr()
});
import DS from 'ember-data';
export default DS.Model.extend({
});
import DS from 'ember-data';
import Item from './item';
export default Item.extend({
title: DS.attr()
});
import DS from 'ember-data';
export default DS.Model.extend({
items: DS.hasMany('item', { polymorphic: true, async: true })
});
{
"version": "0.4.11",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.11/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember-template-compiler.js",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment