Skip to content

Instantly share code, notes, and snippets.

@ericop
Last active October 7, 2016 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericop/1786dffcbf2b91ab7dd39f2f3f0b0f4b to your computer and use it in GitHub Desktop.
Save ericop/1786dffcbf2b91ab7dd39f2f3f0b0f4b to your computer and use it in GitHub Desktop.
Embedded Records with _id
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Embedded Records with _id'
});
import DS from "ember-data";
export default DS.Model.extend({
title: DS.attr(),
author: DS.attr(),
chapters: DS.hasMany('chapter')
});
import DS from "ember-data";
export default DS.Model.extend({
name: DS.attr()
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('books', {
path: '/', resetNamespace: true
}, function () {});
});
export default Router;
import Ember from 'ember';
let storeBooks = {
data: [{
id: 7,
type: 'book',
attributes: {
title: 'War and Peace',
author: 'Leo Tolstoy'
},
relationships: {
chapters: {data: [{
id: 12,
type: 'chapter',
}] }
}
},{
id: 8,
type: 'book',
attributes: {
title: 'Ember-CLI 101',
author: 'Adolfo Builes'
},
relationships: {
chapters: {data: [{
id: 12,
type: 'chapter',
},{
id: 13,
type: 'chapter',
}] }
}
}],
included: [
{
type: 'chapter',
_id: 12,
attributes: {
name: 'An Epic, Amazing Chapter'
}
},
{
type: 'chapter',
_id: 13,
attributes: {
name: 'Dont Repeat Yourself, DRY Code'
}
}
]
};
export default Ember.Route.extend({
model(){
this.get('store').pushPayload(storeBooks);
//this.get('store').push(storeBooks);
//var storedBooks = this.store.findAll('book');
var peekBooks = this.get('store').peekAll('book');
//var queryBooks = this.store.query('book', {ids: [1, 2, 3] });
var firstBook = this.get('store').peekAll('book', 1);
return peekBooks;
},
actions: {
addBook() {
let newChapter = this.get('store').createRecord('chapter', { _id: 99, name: 'The Chapter of the Future'})
let newBook = {
id: 9,
title: 'Some New Book',
author: 'You',
chapters: [newChapter]
};
return this.get('store').createRecord('book', newBook);
}
}
});
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
primaryKey: '_id'
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
background: #faf4f1;
font-size: 12pt;
}
.button-blue {
color: #FFF;
background-color: #33C3F0;
border-color: #33C3F0;
border-radius: 3px;
}
.button-blue:hover {
color: #FFF;
background-color: #1EAEDB;
border-color: #1EAEDB;
}
h1 {
font-family: 'Pacifico', cursive;
color: white;
text-shadow: 1px 1px 1px #000;
background: url('https://static.jsbin.com/custom/emberjs/navigation_background.png') repeat-x;
border-bottom: 1px solid #AA412F;
box-shadow: 1px 1px 1px #000;
background-size:cover;
border-radius: 4px;
}
li {
background-image: linear-gradient(-90deg, #f4d2c1, #e7624b);
border: 1px solid #faf4f1;
border-radius: 4px;
padding: 5px;
}
<h1>{{appName}}</h1>
{{outlet}}
<br>
<h2>Books:</h2>
<ul>
{{#each model as |book|}}
<li>{{book.id}} <strong>{{book.title}}</strong> by {{book.author}}
</li>
<ul>
{{#each book.chapters as |chapter|}}
<li>{{chapter.id}} {{chapter.name}} (chapter)
</li>
{{/each}}
</ul>
{{/each}}
</ul>
<button class="button-blue" type="button"{{action "addBook" this.id}}>Add a Book</button>
{{outlet}}
{
"version": "0.10.5",
"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.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment