-
-
Save runspired/1d37a9df5b421993a7780a90959245c2 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import JSONAPIAdapter from 'ember-data/adapters/json-api'; | |
export default class PostAdapter extends JSONAPIAdapter { | |
async findRecord(store, type, id) { | |
switch (id) { | |
case '1': | |
return { | |
"data": { | |
"id": "1", | |
"type": "posts", | |
"links": { | |
"self": "http:\/\/localhost:3000\/posts\/1" | |
}, | |
"attributes": { | |
"title": "Foo" | |
}, | |
"relationships": { | |
"author": { | |
"links": { | |
"self": "http:\/\/localhost:3000\/posts\/1\/relationships\/author", | |
"related": "http:\/\/localhost:3000\/posts\/1\/author" | |
}, | |
"data": null | |
} | |
} | |
} | |
}; | |
case '2': | |
return { | |
"data": { | |
"id": "2", | |
"type": "posts", | |
"links": { | |
"self": "http:\/\/localhost:3000\/posts\/2" | |
}, | |
"attributes": { | |
"title": "Bar" | |
}, | |
"relationships": { | |
"author": { | |
"links": { | |
"self": "http:\/\/localhost:3000\/posts\/2\/relationships\/author", | |
"related": "http:\/\/localhost:3000\/posts\/2\/author" | |
} | |
} | |
} | |
} | |
}; | |
} | |
} | |
async findBelongsTo(store, snapshot, url) { | |
return { | |
data: null, | |
meta: {}, | |
links: {}, | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { inject as service } from '@ember/service'; | |
export default Ember.Controller.extend({ | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { helper } from '@ember/component/helper'; | |
export default helper(function v(params/*, hash*/) { | |
switch (params[0]) { | |
case undefined: | |
return '[[UNDEFINED]]'; | |
case null: | |
return '[[NULL]]'; | |
default: | |
return params[0]; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
title: attr('string'), | |
author: belongsTo('author', { inverse: 'posts' }), | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { returnSnapshot } from '../serializers/application'; | |
import { all } from 'rsvp'; | |
export default Ember.Route.extend({ | |
async model() { | |
const post1 = await this.store.findRecord('post', 1, { include: 'author' }); | |
const post2 = await this.store.findRecord('post', 2); | |
await post2.belongsTo('author').load(); | |
const snapshot1 = post1.serialize({ [returnSnapshot]: true }); | |
const snapshot2 = post2.serialize({ [returnSnapshot]: true }); | |
console.log(snapshot2); | |
console.log(snapshot1.belongsTo('author')); // logs `null` | |
console.log(snapshot2.belongsTo('author')); // logs `undefined`, expected `null`. | |
return { | |
posts: [post1, post2], | |
authors: [ | |
snapshot1.belongsTo('author'), | |
snapshot2.belongsTo('author') | |
] | |
}; | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import JSONAPISerializer from 'ember-data/serializers/json-api'; | |
export const returnSnapshot = Symbol(); | |
export default JSONAPISerializer.extend({ | |
serialize(snapshot, options) { | |
if (options[returnSnapshot]) { | |
return snapshot; | |
} | |
return this._super(...arguments); | |
}, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.16.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment