Skip to content

Instantly share code, notes, and snippets.

@IBue
Forked from samselikoff/mirage.config.js
Last active May 13, 2020 17:44
Show Gist options
  • Save IBue/243ca2dab9b4615dd67115fbb1d83881 to your computer and use it in GitHub Desktop.
Save IBue/243ca2dab9b4615dd67115fbb1d83881 to your computer and use it in GitHub Desktop.
ember/data issue #4986
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
removeBook: function(record) {
record.set('author', null);
},
unloadBook: function(record) {
record.unloadRecord();
},
reloadAuthors: function() {
this.model.update();
}
}
});
export default function() {
//window.server = this;
this.get('/authors'),
this.get('/books/:id')
};
import { Factory } from 'ember-cli-mirage';
export default Factory.extend({
afterCreate(authorObj) {
authorObj.attrs.name = 'author ' + authorObj.id;
var book = authorObj.createBook();
book.attrs.title = 'book ' + book.id;
book = authorObj.createBook();
book.attrs.title = 'book ' + book.id;
}
});
import { Factory, association } from 'ember-cli-mirage';
export default Factory.extend({
author: association()
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
books: hasMany('book')
});
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
author: belongsTo('author')
});
export default function(server) {
// added this line because global wasn't created otherwise.
server.createList('author', 2);
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
books: DS.hasMany('book')
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.belongsTo('author')
});
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.findAll('author');
}
});
<ul>
{{#each model as |author|}}
<li>{{author.name}} <small>({{author.books.length}} books)</small>
<ol>
{{#each author.books as |book|}}
<li>
{{#if book.isLoading}}
loading…
{{else}}
<span>{{book.title}}</span>
<small>(
<a href='#' {{action (action 'unloadBook' book)}}>unload</a>,
<a href='#' {{action (action 'removeBook' book)}}>remove</a>
)</small>
{{/if}}
</li>
{{/each}}
</ol>
</li>
{{/each}}
</ul>
<p>
<button {{action (action 'reloadAuthors')}}>reload authors</button>
</p>
<p>
Switching <code>ember/data</code> from 2.12 to 2.13 in <code>twiddle.json</code> will reveal the internal error as described in <code>#4986</code> and crash the application when <u>unloading</u> a book from the store.
</p>
{
"version": "0.12.1",
"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/3.2.1/jquery.js",
"ember": "2.12.2",
"ember-template-compiler": "2.12.2"
},
"addons": {
"ember-data": "2.13.1",
"ember-cli-mirage": "0.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment