Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active November 15, 2019 14:21
Show Gist options
  • Save runspired/1ad34bbd30b2e54667496b3a6c296267 to your computer and use it in GitHub Desktop.
Save runspired/1ad34bbd30b2e54667496b3a6c296267 to your computer and use it in GitHub Desktop.
Staggered Record Loading
import Ember from 'ember';
import RSVP from 'rsvp';
export default Ember.Object.extend({
findRecord(_, __, id) {
return new RSVP.Promise(resolve => {
setTimeout(resolve, parseInt(id) * 350);
}).then(() => {
return {
data: {
type: 'user',
id,
attributes: {
name: 'user-' + id
}
}
};
});
},
query() {
return RSVP.resolve({
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' }
]
});
}
});
import Ember from 'ember';
export default Ember.Controller.extend({});
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';
const LazyList = Ember.ArrayProxy.extend({
setContent(content) {
this._loaded = new Set();
this.set('content', content);
},
objectAt(index) {
const record = this.content.objectAt(index);
const { type } = this;
const { id } = record;
const reload = !this._loaded.has(record.id);
if (reload) {
this._loaded.add(record.id)
}
return this.store.findRecord(type, id, { reload });
}
});
export default Ember.Route.extend({
init() {
this._super(...arguments);
this.list = LazyList.create({ type: 'user', store: this.store });
},
model() {
return this.store.query('user', {})
.then(recordArray => {
this.list.setContent(recordArray);
return this.list;
});
}
});
import Ember from 'ember';
export default Ember.Object.extend({
normalizeResponse(_, __, data) { return data; }
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<ol>
{{#each model as |user|}}
<li>{{user.name}}</li>
{{/each}}
</ol>
<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