Skip to content

Instantly share code, notes, and snippets.

@jevanlingen
Forked from pangratz/application.route.js
Last active March 31, 2016 15:20
Show Gist options
  • Save jevanlingen/9a0dd17be2804beb280fa87b71d41c9a to your computer and use it in GitHub Desktop.
Save jevanlingen/9a0dd17be2804beb280fa87b71d41c9a to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/file-types/1', function() {
return json({
'file-types': [{
id: 1,
name: "GIF"
}]
});
});
this.get('/returns-file-type-from-other-api/1', function() {
return json({
fileTypes: [{
id: 1,
name: "GIF"
}]
});
});
this.get('/files/1', function() {
return json({
files: [{
id: 1,
name: 'ape.gif',
type: 1
}]
});
});
});
export default Ember.Route.extend({
model: function() {
return this.store.findRecord('file', 1);
}
});
<div style="font-size: 18px;font-weight:bold">Bug in JSONAPISerializer in combination with RESTSerializer</div>
<hr>
Name: {{model.name}} <br>
Type: {{model.type.name}}
<hr>
Check console for handled requests!
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}`);
}
server.map(function() {
this.get('/people', returnJSON({
data: []
}));
});
};
export default {
name: 'pretender',
initialize
};
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr()
});
import DS from 'ember-data';
const { attr, belongsTo } = DS;
export default DS.Model.extend({
name: attr(),
type: belongsTo('file-type', { async: true })
});
export default DS.RESTSerializer.extend({
isNewSerializerAPI: true
});
export default DS.RESTSerializer.extend({
isNewSerializerAPI: true,
normalize(typeClass, hash, prop) {
if(prop === 'files') {
hash.links = {
type: `/returns-file-type-from-other-api/${hash.type}`
}
}
//calls RESTSerializer normalize();
//RESTSerializer normalizer will call JSONAPISerializer normalize() + return that as result
const jsonApiResponse = this._super(...arguments);
/*
JsonApiResponse has one relationship: type. Type object has following structure:
type: {
data: {
id: "1",
type: "file-type"
},
links: {
related: "/returns-file-type-from-other-system/1"
}
}
*/
//Because both `data` and `links` object exist for the `type` relationship, /file-types/ will be called. Only `link` object was expected.
//So we remove the data object!
delete jsonApiResponse.data.relationships.type.data; //this delete statement should not be needed!
return jsonApiResponse;
}
});
{
"version": "0.4.17",
"EmberENV": {
"FEATURES": {
"ds-references": true
}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.4.3",
"ember-data": "2.4.3",
"ember-template-compiler": "2.4.3",
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment