Created
January 8, 2020 23:32
-
-
Save runspired/c78d076f07f1cd4380f3b4d4888a645c to your computer and use it in GitHub Desktop.
Preflight Query
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 RSVP from 'rsvp'; | |
const RecordArrays = new WeakSet(); | |
const PREFLIGHT_PAYLOAD = { | |
data: [ | |
{ type: 'user', id: '1', }, | |
{ type: 'user', id: '2', }, | |
{ type: 'user', id: '3', }, | |
{ type: 'user', id: '4', }, | |
{ type: 'user', id: '5', }, | |
{ type: 'user', id: '6', }, | |
{ type: 'user', id: '7', }, | |
{ type: 'user', id: '8', }, | |
{ type: 'user', id: '9', }, | |
{ type: 'user', id: '10', }, | |
], | |
meta: { | |
total: 100, | |
pages: 10, | |
currentPage: 1, | |
isPreflight: true, | |
} | |
}; | |
const FULL_PAYLOAD = { | |
data: [ | |
{ type: 'user', id: '1', attributes: { name: 'Chris 1' } }, | |
{ type: 'user', id: '2', attributes: { name: 'Chris 2' } }, | |
{ type: 'user', id: '3', attributes: { name: 'Chris 3' } }, | |
{ type: 'user', id: '4', attributes: { name: 'Chris 4' } }, | |
{ type: 'user', id: '5', attributes: { name: 'Chris 5' } }, | |
{ type: 'user', id: '6', attributes: { name: 'Chris 6' } }, | |
{ type: 'user', id: '7', attributes: { name: 'Chris 7' } }, | |
{ type: 'user', id: '8', attributes: { name: 'Chris 8' } }, | |
{ type: 'user', id: '9', attributes: { name: 'Chris 9' } }, | |
{ type: 'user', id: '10', attributes: { name: 'Chris 10' } }, | |
], | |
meta: { | |
total: 100, | |
pages: 10, | |
currentPage: 1, | |
isPreflight: false | |
} | |
}; | |
export default class ApplicationAdapter { | |
static create() { | |
return new this(); | |
} | |
shouldBackgroundReloadRecord() { | |
return false; | |
} | |
query(store, schema, query, recordArray) { | |
let isInitial = !RecordArrays.has(recordArray); | |
if (isInitial) { | |
RecordArrays.add(recordArray); | |
return this.preflightQuery(store, schema, query, recordArray) | |
.then(result => { | |
console.log(result); | |
setTimeout(() => { | |
recordArray.update(); | |
}, 1000); | |
return result; | |
}); | |
} | |
return this.fulfillQuery(store, schema, query, recordArray); | |
} | |
preflightQuery(store, schema, query, recordArray) { | |
return RSVP.resolve(PREFLIGHT_PAYLOAD); | |
} | |
fulfillQuery(store, schema, query, recordArray) { | |
return new RSVP.Promise(resolve => { | |
setTimeout(() => { resolve(FULL_PAYLOAD); }, 2000); | |
}); | |
} | |
} |
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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
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 Model.extend({ | |
name: attr() | |
}); |
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 Ember from 'ember'; | |
export default Ember.Route.extend({ | |
model() { | |
return this.store.query('user', {}); | |
} | |
}); |
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 { | |
static create() { | |
return new this(); | |
} | |
normalizeResponse(_, __, 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.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.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment