Skip to content

Instantly share code, notes, and snippets.

@aaronfischer
Last active June 6, 2017 09:31
Show Gist options
  • Save aaronfischer/9fb4e28d80767fdb5ccf8ec5aeae6b3b to your computer and use it in GitHub Desktop.
Save aaronfischer/9fb4e28d80767fdb5ccf8ec5aeae6b3b to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
db: Ember.computed('refreshDb', function() {
let dump = window.server.schema.db.dump();
return JSON.stringify(dump, null, 2);
}),
actions: {
toggleIsShowingDatabase() {
this.toggleProperty('isShowingDatabase');
},
refreshDb() {
this.incrementProperty('refreshDb');
},
deleteBook(book) {
book.destroyRecord();
},
deleteAuthor() {
this.get('model').destroyRecord();
}
}
});
import Mirage from 'ember-cli-mirage';
export default function() {
window.server = this;
this.get('authors/:id');
this.delete('authors/:id');
this.delete('books/:id');
this.get('books/:id');
this.patch('authors/:id');
};
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
books: hasMany()
});
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
});
export default function(server) {
let author = server.create('author', {
name: 'John Steinbeck'
});
server.create('book', {
title: 'Test Book',
});
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
books: DS.hasMany()
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
title: attr()
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findRecord('author', 1, {
include: 'books'
});
},
actions: {
addBook() {
let model = this.get('controller.model');
console.log('model', model);
this.store.findRecord('book', 1).then((book) => {
console.log('book', book);
let books = model.get('books');
console.log('books', books);
books.addObject(book);
model.save();
});
},
},
});
body {
margin: 0;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
header {
background: #2e4068;
color: white;
padding: 14px 20px;
}
h1 {
font-size: 12px;
font-weight: bold;
text-shadow: none;
margin: 0 0 2px;
text-transform: uppercase;
opacity: 0.5;
}
h2 {
font-size: 16px;
margin: 0;
}
p {
font-weight: normal;
}
.pa4 {
padding: 20px;
}
.ph4 {
padding-left: 20px;
padding-right: 20px;
}
hr {
border-top: none;
border-bottom: 1px solid #ddd;
}
.panel {
background: whitesmoke;
}
.panel__toggle {
display: block;
text-decoration: none;
padding: 8px;
color: #777;
}
.panel__body {
padding: 8px;
}
<header>
<h1>Mirage</h1>
<h2>One Way Has Many</h2>
</header>
<div class='ph4'>
<p>
<strong>{{model.name}}</strong>'s Books:
</p>
<ul>
{{#each model.books as |book|}}
<li>
{{book.title}}
{{#if book.isSaving}}
<span class='o-50'>[deleting...]</span>
{{else}}
<a href='#' {{action 'deleteBook' book}}>X</a>
{{/if}}
</li>
{{/each}}
</ul>
<p>
<a href='#' {{action 'deleteAuthor'}}>Delete {{model.name}}</a>
</p>
<p>
<a href='#' {{action 'addBook'}}>Add Book</a>
</p>
</div>
<div class='panel'>
<p class='panel__toggle'>
Database dump
</p>
<div class='panel__body'>
<a href='#' {{action 'refreshDb'}}>Refresh db dump</a>
<pre>{{db}}</pre>
</div>
</div>
{
"version": "0.10.7",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {
"ds-finder-include": true
}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.0",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.0",
"ember-testing": "2.10.0"
},
"addons": {
"ember-cli-mirage": "0.3.0-beta.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment