Skip to content

Instantly share code, notes, and snippets.

@thermokarst
Last active April 23, 2016 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thermokarst/18ae6b782e46d39917909a42f2d8cefa to your computer and use it in GitHub Desktop.
Save thermokarst/18ae6b782e46d39917909a42f2d8cefa to your computer and use it in GitHub Desktop.
recheck json api
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import DS from 'ember-data';
export default DS.Model.extend({
post: DS.belongsTo('post'),
message: DS.attr('string'),
user: DS.belongsTo('user'),
ready() {
console.log("before", this.get('id'), this.toJSON());
let names = Ember.get(this.constructor, 'relationshipsByName')._keys.list;
let relationships = this.getProperties.apply(this, names);
Ember.RSVP.hash(relationships).then(()=>{
console.log("after", this.get('id'), this.toJSON());
});
},
});
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('comment'),
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
});
import Ember from 'ember';
export default Ember.Route.extend({
init() {
this._super(...arguments);
$.mockjax({
url: '/posts/1',
responseText: {
data: {
type: "post",
id: 1,
attributes: {
title: "lorem ipsum",
},
relationships: {
comments: {
links: {
related: "/posts/1/comments"
}
}
},
},
}
});
$.mockjax({
url: '/posts/1/comments',
responseText: {
data: [{
type: "comment",
id: 1,
attributes: {
message: "my favorite!",
},
relationships: {
user: {
data: { type: "users", id: 1 },
},
}
}]
}
});
$.mockjax({
url: '/users/1',
responseText: {
data: {
type: "user",
id: 1,
attributes: {
name: "bill",
},
},
},
});
},
model() {
return this.store.findRecord('post', 1);
},
});
<b>{{model.title}}</b>
<ul>
{{#each model.comments as |comment|}}
<li>
{{comment.message}}
(by: {{comment.user.name}})
</li>
{{/each}}
</ul>
{
"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": "release",
"ember-template-compiler": "release",
"jquery-mockjax": "https://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.6.2/jquery.mockjax.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment