Skip to content

Instantly share code, notes, and snippets.

@bmac
Created February 28, 2018 19:25
Show Gist options
  • Save bmac/abbc9e9e9165e5ae05804f165fa5388c to your computer and use it in GitHub Desktop.
Save bmac/abbc9e9e9165e5ae05804f165fa5388c to your computer and use it in GitHub Desktop.
Ember Data RESTAdapter
import Adapter from "ember-data/adapters/rest";
export default Adapter.extend();
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: DS.attr('string'),
books: DS.hasMany('book', { async: false }),
booksAttr: DS.attr()
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
title: DS.attr('string'),
author: DS.belongsTo('author', { async: false })
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/authors', function() {
return json({
books: [
{ id: 1, title: 'Tom Sawyer'},
{ id: 2, title: 'A Farewell To Arms'},
{ id: 3, title: 'The Old Man And the Sea'},
{ id: 4, title: 'The Shining'},
{ id: 5, title: 'The Stand'},
{ id: 6, title: 'The Long Walk'}
],
authors:[
{ id: 1, name: "Mark Twain", books: [1] },
{ id: 2, name: "Ernest Hemingway", books: [2, 3] },
{ id: 3, name: "Stephen King", books: [4, 5, 6]}
]
});
});
});
export default Ember.Route.extend({
model: function() {
return this.get('store').findAll('author');
}
});
import Serializer from "ember-data/serializers/rest";
export default Serializer.extend();
<h2>My Library</h2>
<hr>
{{outlet}}
{{#each model as |author| }}
<section>
<h3>{{author.name}}</h3>
<h5>{{author.booksAttr}}</h5>
<ul>
{{#each author.books as |book|}}
<li>{{book.title}}</li>
{{/each}}
</ul>
</section>
{{/each}}
{
"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": "release",
"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