Skip to content

Commit 80d04d2

Browse files
committed
index: Move data loading back into the controller
This partially reverts commit a34f6c4. The goal of this commit is to display loading spinners for the sections that are still loading, but display the rest of the content of the frontpage already, while the summary data is still loading.
1 parent 664c93e commit 80d04d2

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

app/controllers/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
import Controller from '@ember/controller';
2+
import { computed } from '@ember/object';
3+
import { readOnly } from '@ember/object/computed';
4+
import { inject as service } from '@ember/service';
5+
6+
import { task } from 'ember-concurrency';
27

38
export default Controller.extend({
4-
hasData: true,
9+
fetcher: service(),
10+
11+
model: readOnly('dataTask.lastSuccessful.value'),
12+
13+
hasData: computed('dataTask.{lastSuccessful,isRunning}', function () {
14+
return this.get('dataTask.lastSuccessful') || !this.get('dataTask.isRunning');
15+
}),
16+
17+
dataTask: task(function* () {
18+
let data = yield this.fetcher.ajax('/api/v1/summary');
19+
20+
addCrates(this.store, data.new_crates);
21+
addCrates(this.store, data.most_downloaded);
22+
addCrates(this.store, data.just_updated);
23+
addCrates(this.store, data.most_recently_downloaded);
24+
25+
return data;
26+
}).drop(),
527
});
28+
29+
function addCrates(store, crates) {
30+
for (let i = 0; i < crates.length; i++) {
31+
crates[i] = store.push(store.normalize('crate', crates[i]));
32+
}
33+
}

app/routes/index.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Route from '@ember/routing/route';
22
import { inject as service } from '@ember/service';
33

44
export default Route.extend({
5-
fetcher: service(),
5+
fastboot: service(),
66

77
headTags() {
88
return [
@@ -16,26 +16,12 @@ export default Route.extend({
1616
];
1717
},
1818

19-
setupController(controller, model) {
20-
this._super(controller, model);
19+
setupController(controller) {
2120
this.controllerFor('application').set('searchQuery', null);
22-
},
23-
24-
model() {
25-
return this.fetcher.ajax('/api/v1/summary');
26-
},
2721

28-
// eslint-disable-next-line no-unused-vars
29-
afterModel(model, transition) {
30-
addCrates(this.store, model.new_crates);
31-
addCrates(this.store, model.most_downloaded);
32-
addCrates(this.store, model.just_updated);
33-
addCrates(this.store, model.most_recently_downloaded);
22+
let promise = controller.dataTask.perform();
23+
if (this.fastboot.isFastBoot) {
24+
this.fastboot.deferRendering(promise);
25+
}
3426
},
3527
});
36-
37-
function addCrates(store, crates) {
38-
for (let i = 0; i < crates.length; i++) {
39-
crates[i] = store.push(store.normalize('crate', crates[i]));
40-
}
41-
}

app/styles/index.module.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
display: inline-block;
9494
height: 16px;
9595
width: 16px;
96+
margin-left: 10px;
9697
}
9798
}
9899

0 commit comments

Comments
 (0)