Skip to content

Instantly share code, notes, and snippets.

@jfschaff
Last active April 19, 2018 15:53
Show Gist options
  • Save jfschaff/e962c26923907b707482812362d9e614 to your computer and use it in GitHub Desktop.
Save jfschaff/e962c26923907b707482812362d9e614 to your computer and use it in GitHub Desktop.
nested belongsTo
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 Inflector from 'ember-inflector';
const inflector = Inflector.inflector;
inflector.uncountable('profiles');
export default {};
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
// user: belongsTo('user'),
summary: attr('string'),
});
import Model from "ember-data/model";
import { belongsTo } from "ember-data/relationships";
export default Model.extend({
healthpro: belongsTo('healthpro'),
});
import Ember from 'ember';
import DS from 'ember-data';
import { belongsTo, hasMany } from "ember-data/relationships";
export default DS.Model.extend({
email: DS.attr('string'), // also used as username
profiles: belongsTo('profile'),
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/users', function() {
return json({
users: [
{ id: 1, email: "toto@tata.com", profiles: 1},
]
});
});
this.get('/users/1', function() {
return json({
user: {
id: 1,
email: "toto@tata.com",
profiles: 1
}
});
});
this.get('/profiles', function() {
return json({
profiles: [
{ id: 1, healthpro: 1},
]
});
});
this.get('/profiles/1', function() {
return json({
profile: {
id: 1,
healthpro: 1
}
});
});
this.get('/healthpros', function() {
return json({
healthpros: [
{ id: 1, summary: "User with email address toto@tata.com"},
]
});
});
this.get('/healthpros/1', function() {
return json({
healthpro: {
id: 1,
summary: "User with email address toto@tata.com"
}
});
});
});
export default Ember.Route.extend({
model: function() {
return this.get('store').findRecord('user', 1);
}
});
import Serializer from "ember-data/serializers/rest";
export default Serializer.extend();
<h1>{{model.email}}</h1>
<p>{{model.profiles.healthpro.summary}}</p>
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.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": "2.16.3",
"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