Last active
April 8, 2021 00:50
-
-
Save runspired/9d9c28c2dd8ca260b52487c47459af60 to your computer and use it in GitHub Desktop.
Preloading
This file contains 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
export default class Adapter { | |
constructor(args) { Object.assign(this, args); } | |
static create(args) { return new this(args); } | |
findRecord(store, schema, id, snapshot) { | |
let type = schema.modelName; | |
console.log({ type, id }, snapshot); | |
if (type === 'post') { | |
let authorId = snapshot.belongsTo('author', { id: true }); | |
console.log({ authorId }); | |
let doc = { | |
data: { | |
type, | |
id, | |
attributes: { | |
title: 'first post' | |
}, | |
relationships: { | |
author: { | |
data: { type: 'user', id: '123' } | |
} | |
} | |
} | |
}; | |
console.log({ doc }); | |
return doc; | |
} | |
throw new Error('tried to get author'); | |
} | |
} |
This file contains 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 Controller from '@ember/controller'; | |
export default class ApplicationController extends Controller { | |
appName = 'Ember Twiddle'; | |
} |
This file contains 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 class extends Model { | |
@attr title; | |
@belongsTo('user', { async: true, inverse: 'posts' }) author; | |
} |
This file contains 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 class extends Model { | |
@attr name; | |
@hasMany('post', { async: true, inverse: 'author' }) posts; | |
} |
This file contains 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 Route from '@ember/routing/route'; | |
import { inject } from '@ember/service'; | |
export default class MyRoute extends Route { | |
@inject | |
store; | |
async model() { | |
let record = await this.store.findRecord('post', '1', { include: 'author', preload: { author: 'abc' } }); | |
console.log(record); | |
return record; | |
} | |
} |
This file contains 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
export default class Serializer { | |
constructor(args) { Object.assign(this, args); } | |
static create(args) { return new this(args); } | |
normalizeResponse(_,__, p) { console.log(p); return p; } | |
} |
This file contains 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.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0", | |
"ember-data": "3.18.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment