Last active
November 15, 2019 14:21
-
-
Save runspired/1ad34bbd30b2e54667496b3a6c296267 to your computer and use it in GitHub Desktop.
Staggered Record Loading
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'; | |
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' } | |
] | |
}); | |
} | |
}); |
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({}); |
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'; | |
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; | |
}); | |
} | |
}); |
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.Object.extend({ | |
normalizeResponse(_, __, data) { return data; } | |
}); |
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