Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mirage 0.3 - Many to Many, with factories
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');
},
deleteModel(model) {
model.destroyRecord();
}
}
});
import Mirage from 'ember-cli-mirage';
export default function() {
window.server = this;
this.get('authors');
this.get('authors/:id');
this.delete('authors/:id');
this.get('books');
this.delete('books/:id');
};
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
name() {
return faker.name.findName();
}
});
import { Factory, faker } from 'ember-cli-mirage';
export default Factory.extend({
title() {
return faker.company.catchPhrase();
}
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
books: hasMany()
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
authors: hasMany()
});
import { faker } from 'ember-cli-mirage';
export default function(server) {
let authors = server.createList('author', 6);
let books = server.createList('book', 5);
books.forEach((book) => {
let numAuthors = faker.random.number({min: 1, max: 3});
let authorsOfBook = randomSubset(authors, numAuthors);
book.authors = authorsOfBook;
book.save();
});
function randomSubset (arr, n) {
if (n >= arr.length) { return arr; }
if (arr.length === 0) { return arr; }
if (n < 1) { n = 1; }
let selected = [];
while (selected.length < n) {
let candidate = faker.random.arrayElement(arr);
if (!selected.includes(candidate)) {
selected.push(candidate);
}
}
return selected;
}
}
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 DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr(),
authors: DS.hasMany()
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return Ember.RSVP.hash({
authors: this.store.findAll('author'),
books: this.store.findAll('book')
});
},
setupController: function(controller, models) {
let authors = models.authors;
let books = models.books;
controller.set('authors', authors);
controller.set('books', books);
}
});
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>Many to Many, with Factories</h2>
</header>
<div class='ph4'>
<h1>Authors</h1>
{{#each authors as |author|}}
<p>
<strong>{{author.name}}</strong>'s Books:
</p>
<ul>
{{#each author.books as |book|}}
<li>
{{book.title}}
{{#if book.isSaving}}
<span class='o-50'>[deleting...]</span>
{{else}}
<a href='#' {{action 'deleteModel' book}}>X</a>
{{/if}}
</li>
{{/each}}
</ul>
<p>
<a href='#' {{action 'deleteModel' author}}>Delete {{author.name}}</a>
</p>
{{/each}}
</div>
<hr>
<div class='ph4'>
<h1>Books</h1>
{{#each books as |book|}}
<p>
<strong>{{book.title}}</strong>'s Authors:
</p>
<ul>
{{#each book.authors as |author|}}
<li>
{{author.name}}
{{#if author.isSaving}}
<span class='o-50'>[deleting...]</span>
{{else}}
<a href='#' {{action 'deleteModel' author}}>X</a>
{{/if}}
</li>
{{/each}}
</ul>
<p>
<a href='#' {{action 'deleteModel' book}}>Delete "{{book.title}}"</a>
</p>
{{/each}}
</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>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance | Users');
test('I can see the users', function(assert) {
server.create('user', { name: 'Link' });
//visit('/');
andThen(function() {
assert.ok(true);
//assert.ok(find(':contains(Welcome Link!)').length);
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
destroyApp(this.application);
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
export default function startApp(attrs) {
let application;
let attributes = Ember.merge({rootElement: "#test-root"}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
Ember.run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"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.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.