Skip to content

Instantly share code, notes, and snippets.

@samselikoff
Last active May 18, 2018 20:52
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 samselikoff/31c478d752db090dc89c1593e6b022f1 to your computer and use it in GitHub Desktop.
Save samselikoff/31c478d752db090dc89c1593e6b022f1 to your computer and use it in GitHub Desktop.
Ember Data: Data becoming unlinked
export default function() {
//window.server = this;
this.get('authors/:id');
};
export default function(server) {
let author = server.create('author', {
name: 'John Steinbeck'
});
['Of Mice and Men', 'The Grapes of Wrath', 'Travels with Charley'].forEach(title => {
server.create('author-book', {
book: server.create('book', { title, isPublished: true }),
author
});
});
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
links() {
return {
book: {
self: '/some/url',
related: '/some/url'
}
};
}
});
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
links() {
return {
authorBooks: {
self: '/some/url',
related: '/some/url'
}
};
}
});
import DS from 'ember-data';
export default DS.Model.extend({
author: DS.belongsTo({ async: false }),
book: DS.belongsTo({ async: false })
});
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
name: DS.attr(),
authorBooks: DS.hasMany('author-book', { async: false }),
books: Ember.computed.mapBy('authorBooks', 'book'),
publishedBooks: Ember.computed('books.@each.isPublished', function() {
let books = this.get('books');
console.log(books);
return books.filterBy('isPublished');
})
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr(),
isPublished: DS.attr(),
authorBooks: DS.hasMany('author-book', { async: false })
});
import Ember from 'ember';
export default Ember.Route.extend({
beforeModel() {
return this.store.findRecord('author', 1, {
include: 'author-books.book',
reload: true
});
},
model() {
// CHANGEME
let showBug = true;
if (!showBug) {
// Reloading the author is fine...
return this.store.findRecord('author', 1, {
include: 'author-books.book',
reload: true
});
} else {
// But if we reload the author without including author-books.book,
//the existing books are disassociated from the author
return this.store.findRecord('author', 1, {
include: 'author-books',
reload: true
});
}
},
afterModel(author) {
let books = author.get('authorBooks').mapBy('book');
// The bug is that author.authorBooks.mapBy('book') is null,
// even though the data was there from beforeModel.
//
// The book link in the response payload is overriding the relationship.
console.log('The authors books:');
console.log(books);
}
});
body {
margin: 0;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
h1 {
font-size: 12px;
font-weight: bold;
text-align: center;
text-shadow: none;
background: #2e4068;
color: white;
padding: 20px;
margin: 0;
text-transform: uppercase;
}
p {
font-weight: normal;
}
.pa4 {
padding: 20px;
}
hr {
border-top: none;
border-bottom: 1px solid #ddd;
}
button {
padding: 10px 20px;
background: #3490DC;
color: white;
box-shadow: 0 2px 0px 0px #1C3D5A;
border: none;
border-radius: 2px;
cursor: pointer;
}
button:hover {
background: #2779BD;
}
<h1>Mirage boilerplate</h1>
<div class='pa4'>
<p>
Open the console.
</p>
</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.13.1",
"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": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3",
"ember-cli-mirage": "0.4.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment