Skip to content

Instantly share code, notes, and snippets.

@k-fish
Created October 24, 2016 13:03
Show Gist options
  • Save k-fish/3812c90228b16779d3ce54c6e6f8b109 to your computer and use it in GitHub Desktop.
Save k-fish/3812c90228b16779d3ce54c6e6f8b109 to your computer and use it in GitHub Desktop.
Working Ember Data 1.13 Embedded Records
import Ember from 'ember';
import DS from 'ember-data';
const { RSVP } = Ember;
export default DS.RESTAdapter.extend({
findAll(store, type, sinceToken, snapshotRecordArray) {
return RSVP.resolve({
posts: [{
id: 1,
author: {
id: 2,
name: 'lovelace'
}
}]
});
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import DS from 'ember-data';
const { attr, belongsTo } = DS;
export default DS.Model.extend({
name: attr(),
authorid: attr()
});
import DS from 'ember-data';
const { belongsTo, Model } = DS;
export default Model.extend({
author: belongsTo('author')
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
posts: this.store.findAll('post')
});
}
});
import DS from 'ember-data';
const { RESTSerializer, EmbeddedRecordsMixin } = DS;
export default RESTSerializer.extend(
EmbeddedRecordsMixin,
{
isNewSerializerAPI: true,
attrs: {
'author': { serialize: 'records', deserialize: 'records' }
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{#each model.posts as |post|}}
{{post.author.name}}
{{/each}}
<br>
<br>
{
"version": "0.10.6",
"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.9.0",
"ember-data": "1.13.11",
"ember-template-compiler": "2.9.0",
"ember-testing": "2.9.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment