Skip to content

Instantly share code, notes, and snippets.

@pangratz
Created October 21, 2016 16:04
Show Gist options
  • Save pangratz/6038af7ad1202c6b6e32c8af0b4e1306 to your computer and use it in GitHub Desktop.
Save pangratz/6038af7ad1202c6b6e32c8af0b4e1306 to your computer and use it in GitHub Desktop.
import DS from 'ember-data';
import Adapter from "ember-data/adapters/rest";
const { errorsHashToArray } = DS;
export default Adapter.extend({
handleResponse(status, headers, payload) {
if (this.isInvalid(...arguments)) {
payload.errors = errorsHashToArray(payload.errors);
}
return this._super(...arguments);
}
});
export function returnJSON(status, body) {
return json(...arguments);
};
export function json(status, body) {
if (arguments.length === 1) {
body = status;
status = 200;
}
return [
status,
{ "Content-Type": "application/json" },
JSON.stringify(body)
];
};
export const server = new Pretender();
export function initialize() {
server.handledRequest = function(verb, path, request) {
console.log(`handled request to ${verb} ${path}`);
}
server.unhandledRequest = function(verb, path, request) {
console.log(`undhandled request ${verb} ${path}`);
}
};
export default {
name: 'pretender',
initialize
};
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()
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.post('/people', function() {
return json(422, {
"errors": {
"email": ["can't be blank"],
"password": ["can't be blank"],
"first_name": ["can't be blank"],
"last_name": ["can't be blank"],
"title": ["can't be blank", "Should be Mr or Ms"]
}
});
});
});
export default Ember.Route.extend({
model: function() {
return this.store.createRecord('person');
},
setupController(controller, model) {
this._super(...arguments);
model.save();
}
});
import Serializer from "ember-data/serializers/json";
export default Serializer.extend();
<ul>
{{#each model.errors as |error|}}
<li>{{error.attribute}} {{error.message}}</li>
{{/each}}
</ul>
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {
}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "release",
"ember-data": "canary",
"ember-template-compiler": "release",
"route-recognizer": "https://rawgit.com/tildeio/route-recognizer/56f5fcec6ae58d8e86b5dc77609809fb91198142/dist/route-recognizer.js",
"FakeXMLHttpRequest": "https://rawgit.com/pretenderjs/FakeXMLHttpRequest/23c3a96b5b24f1bfe595867437e4f128a29c2840/fake_xml_http_request.js",
"pretender": "https://rawgit.com/pretenderjs/pretender/c6de9afe18b1472aded2592f5a80ad9a26a0e262/pretender.js"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment