Skip to content

Instantly share code, notes, and snippets.

@runspired
Created January 8, 2020 23:32
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 runspired/c78d076f07f1cd4380f3b4d4888a645c to your computer and use it in GitHub Desktop.
Save runspired/c78d076f07f1cd4380f3b4d4888a645c to your computer and use it in GitHub Desktop.
Preflight Query
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);
});
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
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()
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.query('user', {});
}
});
export default class Serializer {
static create() {
return new this();
}
normalizeResponse(_, __, p) {
return p;
}
}
<h1>Welcome to {{appName}}</h1>
<br>
Total: {{model.meta.total}}<br>
Current Page: {{model.meta.currentPage}}<br>
Status: {{if model.isLoaded (if model.isUpdating 'updating' (if model.meta.isPreflight 'partially loaded' 'fully loaded')) 'loading'}}
<br>
<br>
<ul>
{{#each model as |user|}}
<li>{{user.id}} : {{user.name}}</li>
{{/each}}
</ul>
<br>
{{outlet}}
<br>
<br>
{
"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