Skip to content

Instantly share code, notes, and snippets.

@mupkoo
Last active May 18, 2017 14:43
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 mupkoo/f1b8282489a0b226615ac1961e96761e to your computer and use it in GitHub Desktop.
Save mupkoo/f1b8282489a0b226615ac1961e96761e to your computer and use it in GitHub Desktop.
Update-Delete-Create
import Ember from 'ember';
export default Ember.Controller.extend({
_counter: 1,
actions: {
modifyChildren() {
let parent = this.get('model');
parent.get('children').then((children) => {
let first = children.get('firstObject');
let last = children.get('lastObject');
let data = [
{ id: first.get('id') },
{ id: last.get('id'), isDeleted: true },
{ isNew: true }
];
return this._updateChildren(parent, this._counter, data);
}).then(() => {
this._counter++;
});
}
},
_updateChildren(parent, counter, data) {
data = data.map(({ id, isNew, isDeleted }) => {
if (isNew) {
return this.store.createRecord('child', {
parent,
name: `Created - ${counter}`
}).save();
} else if (isDeleted) {
return this.store.peekRecord('child', id).destroyRecord();
} else {
let record = this.store.peekRecord('child', id);
record.set('name', `Updated - ${counter}`);
return record.save();
}
});
return Ember.RSVP.all(data);
}
});
export default function() {
this.get('/parents/:id');
this.get('/parents/:id/children', function ({ children }) {
let json = this.serialize(children.all());
return json;
});
this.get('/children/:id');
this.post('/children');
this.patch('/children/:id');
this.del('/children/:id');
};
import { Factory, faker, trait } from 'ember-cli-mirage';
export default Factory.extend({
name() {
return faker.name.findName();
}
});
import { Factory, faker, trait } from 'ember-cli-mirage';
export default Factory.extend({
name() {
return faker.name.findName();
}
});
import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
export default Model.extend({
parent: belongsTo()
});
import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
export default Model.extend({
children: hasMany()
});
export default function(server) {
let parent = server.create('parent');
server.createList('child', 2, { parent });
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
links(parent) {
return {
'children': {
related: `/parents/${parent.id}/children`
}
};
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string'),
parent: belongsTo('parent')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string'),
children: hasMany('child')
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findRecord('parent', 1);
}
});
<h1>Add/Update/Delete relation</h1>
<p>Parent {{ model.name }} has:</p>
<ul>
{{#each model.children as |child|}}
<li>{{ child.name }}</li>
{{/each}}
</ul>
<button type="button" onclick={{ action "modifyChildren" }}>
Modify children
</button>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Modify multiple relation models');
test('smoke test', function(assert) {
visit('/');
click('button');
andThen(function() {
assert.equal($('#test-root li').length, 2);
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => 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';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
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.12.1",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"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