Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mirage Nested Relationships
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
export default function() {
this.get('assets');
this.get('assets/:id/images', (schema, request) => {
let asset = schema.asset.find(request.params.id);
return asset.images;
});
this.get('images');
this.get('images/:id');
};
import Mirage from 'ember-cli-mirage';
export default Mirage.Factory.extend({
});
import Mirage from 'ember-cli-mirage';
export default Mirage.Factory.extend({
});
import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
export default Model.extend({
image: belongsTo(),
images: hasMany()
});
import { Model } from 'ember-cli-mirage';
export default Model.extend({
});
export default function(server) {
// added this line because global wasn't created otherwise.
window.server = server;
let image = server.create('image', {title: 'Image Title 1'});
let images = [];
for (var i = 5; i >= 0; i--) {
images.push(server.create('image', {title: 'Image Title ' + i}));
}
server.create('asset', { image: image, images: images, title: 'Asset title' });
}
import JsonApiSerializer from 'ember-cli-mirage/serializers/json-api-serializer';
export default JsonApiSerializer;
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
serialize(response, request) {
let json = ApplicationSerializer.prototype.serialize.apply(this, arguments);
json.data.forEach(object => {
object.relationships.images = {
related: `/assets/${object.id}/images`
};
});
return json;
}
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr(),
asset: DS.belongsTo('asset', {async:true}),
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr(),
image: DS.belongsTo('asset/image', {async:true}),
images: DS.hasMany('asset/image', {async:true})
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('asset');
}
});
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend();
<h1>Welcome to {{appName}}</h1>
{{#each model as |row|}}
<h2>{{row.title}}</h2>
<h3>{{row.image.title}}</h3>
<ul>
{{#each row.images as |image|}}
<li>{{image.title}}</li>
{{/each}}
</ul>
{{/each}}
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": {}
},
"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.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment