Skip to content

Instantly share code, notes, and snippets.

@0000marcell
Last active June 19, 2020 05:13
Show Gist options
  • Save 0000marcell/7062d70d2eba3ba32a8f448cd50b2b2d to your computer and use it in GitHub Desktop.
Save 0000marcell/7062d70d2eba3ba32a8f448cd50b2b2d to your computer and use it in GitHub Desktop.
JSONAPI Order
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
export default function() {
//window.server = this;
 this.post('/orders', (schema, request) => {
   alert(`received on the backend: ${JSON.stringify(request.requestBody)}`);
});
}
import Model from "ember-data/model";
import attr from "ember-data/attr";
export default Model.extend({
 shippingFirstName: attr('string'),
shippingLastName: attr('string')
});
import Ember from 'ember';
export default Ember.Route.extend({
model(){
   return this.store.createRecord('order');
},
actions: {
create(){
let model = this.get('controller.model');
model.save();
}
}
});
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
serialize(snapshot, options) {
let json = this._super(...arguments);
alert(`before serialize: ${JSON.stringify(json)}`);
json.shipping_to = {
first_name: json.data.attributes['shipping-first-name'],
last_name: json.data.attributes['shipping-last-name']
};
delete json.data;
delete json.type;
alert(`after serialize: ${JSON.stringify(json)}`)
return json;
}
});
<h1>Create new order</h1>
{{input
value=model.shippingFirstName
placeholder="first name"}}
{{input
value=model.shippingLastName
placeholder="last name"}}
<button {{action "create"}}>create order</button>
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
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.13.0",
"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.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment