Skip to content

Instantly share code, notes, and snippets.

@Bartheleway
Last active April 9, 2018 19:43
Show Gist options
  • Save Bartheleway/8e049802cbdf7e201f7f0e9931d76b67 to your computer and use it in GitHub Desktop.
Save Bartheleway/8e049802cbdf7e201f7f0e9931d76b67 to your computer and use it in GitHub Desktop.
Fail to fetch
import Adapter from "ember-data/adapters/json-api";
export default Adapter.extend();
import Ember from 'ember';
export default Ember.Controller.extend({
personWithoutGroup: Ember.computed(function() {
return this.get('store').findRecord('person', 1);
}),
personWithGroup: Ember.computed(function() {
return this.get('store').findRecord('person', 2);
}),
actions: {
setgroup(record) {
this.get('store').findRecord('group', 1).then(g => record.set('group', g));
},
unsetgroup(record) {
record.set('group', null);
},
reload(record) {
this.get('store').peekRecord('person', record.get('id')).reload();
},
query(record) {
this.get('store').findRecord('person', record.get('id'));
}
}
});
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: 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: attr('string'),
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string'),
group: belongsTo('group', { inverse: null }),
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/groups/1', function() {
return json({
data: {
type: 'group',
id: 1,
attributes: {
name: 'This is a group'
}
}
});
});
this.get('/groups/2', function() {
return json({
data: {
type: 'group',
id: 2,
attributes: {
name: 'This is a group 2'
}
}
});
});
this.get('/people/1', function() {
return json({
data: {
type: 'person',
id: 1,
attributes: {
name: 'Person without group'
}
}
});
});
this.get('/people/2', function() {
return json({
data: {
type: 'person',
id: 2,
attributes: {
name: 'Person with group'
},
relationships: {
group: {
data: {
type: 'group',
id: 1,
}
}
}
}
});
});
});
export default Ember.Route.extend({
});
import Serializer from "ember-data/serializers/json-api";
export default Serializer.extend();
Name: {{personWithoutGroup.name}}<br>
Group: {{if personWithoutGroup.group personWithoutGroup.group.name 'No group'}}<br>
<button {{action 'setgroup' personWithoutGroup}}>Set</button>
<button {{action 'reload' personWithoutGroup}}>Reload</button>
<button {{action 'query' personWithoutGroup}}>Query</button>
<br>
<br>
Name: {{personWithGroup.name}}<br>
Group: {{if personWithGroup.group personWithGroup.group.name 'No group'}}<br>
<button {{action 'unsetgroup' personWithGroup}}>Unset</button>
<button {{action 'reload' personWithGroup}}>Reload</button>
<button {{action 'query' personWithGroup}}>Query</button>
{
"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.16.0",
"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": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment