Skip to content

Instantly share code, notes, and snippets.

@Gaurav0
Forked from karellm/adapters.application.js
Last active February 9, 2017 03:07
Show Gist options
  • Save Gaurav0/ecb7e10e52c7dab9e9cc8e214303f9bb to your computer and use it in GitHub Desktop.
Save Gaurav0/ecb7e10e52c7dab9e9cc8e214303f9bb to your computer and use it in GitHub Desktop.
Relationships
import Adapter from "ember-data/adapters/json-api";
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: true })
});
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')
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('author', { path: '/:authorId' }, function () {
this.route('books', { path: '/' });
});
});
export default Router;
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/authors', function() {
return json({
data: [
{
id: 1,
type: 'author',
attributes: {
name: 'Author 1'
},
relationships: {
books: {
links: {
related: '/authors/1/books'
}
}
}
},
{
id: 2,
type: 'author',
attributes: {
name: 'Author 2'
},
relationships: {
books: {
links: {
related: '/authors/2/books'
}
}
}
},
{
id: 3,
type: 'author',
attributes: {
name: 'Author 3'
},
relationships: {
books: {
links: {
related: '/authors/3/books'
}
}
}
}
]
});
});
});
export default Ember.Route.extend({
model: function() {
return this.get('store').findAll('author');
}
});
import Ember from 'ember';
import { server, json } from '../../initializers/pretender';
server.map(function() {
this.get('/authors/:id/books', function(request) {
var id = request.params.id;
return json({
data: [
{
id: '1-' + id,
type: 'book',
attributes: {
title: 'Book 1 of Author ' + id
}
},
{
id: '2-' + id,
type: 'book',
attributes: {
title: 'Book 2 of Author ' + id
}
}
]
});
});
});
export default Ember.Route.extend({
model() {
const author = this.modelFor('author');
return author.get('books');
}
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
const { RSVP } = Ember;
server.map(function() {
this.get('/authors/:id', function(request) {
var id = request.params.id;
return json({
data: {
id: id,
type: 'author',
attributes: {
name: 'Author ' + id
},
relationships: {
books: {
links: [{
related: '/author/' + id + '/books'
}]
}
}
}
});
});
});
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('author', params.authorId);
}
});
import Serializer from "ember-data/serializers/json-api";
export default Serializer.extend();
<h2>My Library</h2>
<hr>
{{#each model as |author| }}
<p>
{{link-to author.name "author.books" author.id}}
</p>
{{/each}}
{{outlet}}
<h3>{{model.length}} Books:</h3>
<ul>
{{#each model as |book|}}
<li>
{{book.title}}
</li>
{{/each}}
</ul>
{{outlet}}
<h2>{{model.name}}</h2>
{{outlet}}
{
"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": "2.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"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