Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Last active May 30, 2016 17:51
Show Gist options
  • Save samselikoff/38497938416405dbdce827b8fa75c757 to your computer and use it in GitHub Desktop.
Save samselikoff/38497938416405dbdce827b8fa75c757 to your computer and use it in GitHub Desktop.
Mirage 0.2 many to many
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
export default function() {
window.server = this;
this.get('users/:id');
};
import { Factory } from 'ember-cli-mirage';
export default Factory;
import { Model } from 'ember-cli-mirage';
export default Model.extend({
});
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
user: belongsTo(),
role: belongsTo()
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
userRoles: hasMany()
});
export default function(server) {
// added this line because global wasn't created otherwise.
window.server = server;
let admin = server.create('role', { title: 'admin' });
let editor = server.create('role', { title: 'editor' });
let user = server.create('user', { email: 'foo@bar.com' });
server.create('user-role', { user, role: admin });
server.create('user-role', { user, role: editor });
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import DS from 'ember-data';
export default DS.Model.extend({
});
import DS from 'ember-data';
export default DS.Model.extend({
email: DS.attr(),
roles: DS.hasMany()
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findRecord('user', 1, { include: 'roles' });
}
});
<h1>Mirage many to many</h1>
<p>The user {{model.email}} has {{model.roles.length}} roles</p>
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.7.2",
"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": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.4/ember-template-compiler.js"
},
"addons": {
"ember-cli-mirage": "0.2.0-beta.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment