Skip to content

Instantly share code, notes, and snippets.

@adam-knights
Last active May 10, 2016 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adam-knights/6deb122bcb20130eb1f7dfed1a867a50 to your computer and use it in GitHub Desktop.
Save adam-knights/6deb122bcb20130eb1f7dfed1a867a50 to your computer and use it in GitHub Desktop.
New Twiddle
import DS from "ember-data";
export default DS.JSONAPIAdapter.extend();
import Ember from 'ember';
export default Ember.Component.extend({
allPets: Ember.computed('people.@each.pets', function() {
let people = this.get('people');
let pets = [];
people.forEach(p => pets.pushObjects(p.get('pets').toArray()));
return pets;
}),
missingPetNames: Ember.computed('allPets.@each.name', function() {
let requiredPets = ['Felix', 'Jonny'];
let allPets = this.get('allPets');
let missingPetNames = [];
console.log(allPets.get('firstObject.id'));
requiredPets.forEach(p => {
if (!allPets.any(a => a.get('name') === p)) {
missingPetNames.pushObject(p);
}
});
return missingPetNames;
})
});
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 DS from "ember-data";
import { hasMany } from 'ember-data/relationships';
const { Model, attr } = DS;
export default Model.extend({
name: attr(),
pets: hasMany('pet', {async: true})
});
import DS from "ember-data";
import { belongsTo } from 'ember-data/relationships';
const { Model, attr } = DS;
export default Model.extend({
name: attr(),
person: belongsTo('person', {async: true})
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/people', function() {
return json({
data: [{
type: 'person',
id: 1,
attributes: {
name: 'George Michael Bluth'
},
relationships: {
pets: {
data: [
{
id: 122,
type: 'pet'
},
{
id: 138,
type: 'pet'
}
]
}
}
},
{
type: 'person',
id: 2,
attributes: {
name: 'Adam K'
},
relationships: {
pets: {
data: [
{
id: 203,
type: 'pet'
}
]
}
}
}]
});
});
this.get('/pets/122', function() {
return json({
data: {
type: 'pet',
id: 122,
attributes: {
name: 'Snoopy'
}
}
});
});
this.get('/pets/138', function() {
return json({
data: {
type: 'pet',
id: 138,
attributes: {
name: 'Scooby'
}
}
});
});
this.get('/pets/203', function() {
return json({
data: {
type: 'pet',
id: 203,
attributes: {
name: 'Felix'
}
}
});
});
});
export default Ember.Route.extend({
model: function() {
return this.store.findAll('person');
}
});
import DS from "ember-data";
export default DS.JSONAPISerializer.extend();
{{#each model as |person id|}}
<p>Name: {{person.name}}</p>
{{/each}}
<h4>All the pets:</h4>
{{the-pets people=model}}
{{#each allPets as |pet id|}}
<p><i>{{pet.name}}</i></p>
{{/each}}
<h4>Missing Pets</h4>
{{#each missingPetNames as |name|}}
<p>{{name}}</p>
{{/each}}
{
"version": "0.7.2",
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment