Skip to content

Instantly share code, notes, and snippets.

@pangratz
Last active October 5, 2016 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pangratz/9019e034392dc2e61aea to your computer and use it in GitHub Desktop.
Save pangratz/9019e034392dc2e61aea to your computer and use it in GitHub Desktop.
ds-references
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
loadFamily: function() {
var personRef = this.store.getReference('person', 1);
var person = personRef.value();
var familyRef = person.belongsTo('family');
if (!familyRef.value()) {
familyRef.load().then((family) => {
this.set('model.family', family);
});
}
}
}
});
import Ember from 'ember';
import { server, json } from '../initializers/pretender';
server.map(function() {
this.get('/people/1', function() {
return json({
data: {
type: 'person',
id: 1,
attributes: {
name: 'George Michael Bluth'
},
relationships: {
family: {
data: {
type: 'family',
id: 1
}
}
}
}
});
});
this.get('/families/1', function() {
return json({
data: {
type: 'family',
id: 1,
attributes: {
name: "Bluths"
}
}
});
});
});
export default Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
person: this.store.findRecord('person', 1)
});
},
actions: {
reload: function() {
var personRef = this.store.getReference('person', 1);
personRef.reload();
}
}
});
Name: {{model.person.name}} <br>
{{#if model.family }}
Family: {{model.family.name}}
{{else}}
Family: family not yet loaded <button {{action "loadFamily" }}>loadFamily</button>
{{/if}}
<hr>
<button {{action "reload" }}>reload</button>
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() {
};
export default {
name: 'pretender',
initialize
};
import DS from 'ember-data';
const { attr, hasMany } = DS;
export default DS.Model.extend({
persons: hasMany(),
name: attr()
});
import DS from 'ember-data';
const { attr, belongsTo } = DS;
export default DS.Model.extend({
name: attr(),
family: belongsTo()
});
{
"version": "0.4.17",
"EmberENV": {
"FEATURES": {
"ds-references": true
}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "canary",
"ember-data": "canary",
"ember-template-compiler": "canary",
"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/f52b8063665420a244ca4547e3eb57b09e61acf7/pretender.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment