Skip to content

Commit 16460d2

Browse files
committed
Auto merge of #2429 - Turbo87:dashboard, r=locks
dashboard: Cleanup data loading code Best reviewed commit-by-commit :) r? @locks
2 parents 77cb55a + a8a5b03 commit 16460d2

File tree

4 files changed

+31
-46
lines changed

4 files changed

+31
-46
lines changed

app/controllers/dashboard.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { A } from '@ember/array';
22
import Controller from '@ember/controller';
33
import { computed } from '@ember/object';
4+
import { alias } from '@ember/object/computed';
5+
6+
import { task } from 'ember-concurrency';
47

58
import ajax from '../utils/ajax';
69

@@ -10,14 +13,14 @@ export default Controller.extend({
1013
init() {
1114
this._super(...arguments);
1215

13-
this.loadingMore = false;
1416
this.hasMore = false;
15-
this.myCrates = A();
16-
this.myFollowing = A();
1717
this.myFeed = A();
18-
this.myStats = 0;
1918
},
2019

20+
myCrates: alias('model.myCrates'),
21+
myFollowing: alias('model.myFollowing'),
22+
myStats: alias('model.myStats'),
23+
2124
visibleCrates: computed('myCrates.[]', function () {
2225
return this.myCrates.slice(0, TO_SHOW);
2326
}),
@@ -34,20 +37,13 @@ export default Controller.extend({
3437
return this.get('myFollowing.length') > TO_SHOW;
3538
}),
3639

37-
actions: {
38-
async loadMore() {
39-
this.set('loadingMore', true);
40-
let page = this.myFeed.length / 10 + 1;
41-
42-
try {
43-
let data = await ajax(`/api/v1/me/updates?page=${page}`);
44-
let versions = data.versions.map(version => this.store.push(this.store.normalize('version', version)));
45-
46-
this.myFeed.pushObjects(versions);
47-
this.set('hasMore', data.meta.more);
48-
} finally {
49-
this.set('loadingMore', false);
50-
}
51-
},
52-
},
40+
loadMoreTask: task(function* () {
41+
let page = this.myFeed.length / 10 + 1;
42+
43+
let data = yield ajax(`/api/v1/me/updates?page=${page}`);
44+
let versions = data.versions.map(version => this.store.push(this.store.normalize('version', version)));
45+
46+
this.myFeed.pushObjects(versions);
47+
this.set('hasMore', data.meta.more);
48+
}),
5349
});

app/routes/dashboard.js

+12-24
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,22 @@ import RSVP from 'rsvp';
55
import AuthenticatedRoute from '../mixins/authenticated-route';
66

77
export default Route.extend(AuthenticatedRoute, {
8-
setupController(controller) {
9-
this._super(...arguments);
8+
async model() {
9+
let user = this.session.currentUser;
1010

11-
controller.set('myCrates', this.get('data.myCrates'));
12-
controller.set('myFollowing', this.get('data.myFollowing'));
13-
controller.set('myStats', this.get('data.myStats'));
14-
15-
if (!controller.loadingMore) {
16-
controller.set('myFeed', A());
17-
controller.send('loadMore');
18-
}
19-
},
11+
let myCrates = this.store.query('crate', { user_id: user.get('id') });
12+
let myFollowing = this.store.query('crate', { following: 1 });
13+
let myStats = user.stats();
2014

21-
model() {
22-
return this.get('session.currentUser');
15+
return await RSVP.hash({ myCrates, myFollowing, myStats });
2316
},
2417

25-
async afterModel(user) {
26-
let myCrates = this.store.query('crate', {
27-
user_id: user.get('id'),
28-
});
29-
30-
let myFollowing = this.store.query('crate', {
31-
following: 1,
32-
});
33-
34-
let myStats = user.stats();
18+
setupController(controller) {
19+
this._super(...arguments);
3520

36-
this.set('data', await RSVP.hash({ myCrates, myFollowing, myStats }));
21+
if (!controller.isRunning) {
22+
controller.set('myFeed', A());
23+
controller.loadMoreTask.perform();
24+
}
3725
},
3826
});

app/styles/dashboard.module.css

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
}
123123

124124
.load-more {
125+
display: block;
125126
text-align: center;
126127
width: 100%;
127128
padding: 10px;

app/templates/dashboard.hbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@
6060
{{/each}}
6161
</ul>
6262

63-
{{#if this.loadingMore}}
63+
{{#if this.loadMoreTask.isRunning}}
6464
<span local-class='load-more'>
6565
<img src="/assets/ajax-loader.gif">
6666
</span>
6767
{{else}}
6868
{{#if this.hasMore}}
69-
<button type="button" local-class='load-more' {{ action 'loadMore' }}>
69+
<button type="button" local-class='load-more' {{on "click" (perform this.loadMoreTask)}}>
7070
Load More
7171
</button>
7272
{{/if}}

0 commit comments

Comments
 (0)