File tree 3 files changed +51
-30
lines changed
3 files changed +51
-30
lines changed Original file line number Diff line number Diff line change 1
1
import Controller from '@ember/controller' ;
2
- import { computed } from '@ember/object' ;
3
- import { readOnly } from '@ember/object/computed' ;
4
-
5
- import ajax from 'ember-fetch/ajax' ;
6
- import { task } from 'ember-concurrency' ;
7
2
8
3
export default Controller . extend ( {
9
- model : readOnly ( 'dataTask.lastSuccessful.value' ) ,
10
-
11
- hasData : computed ( 'dataTask.{lastSuccessful,isRunning}' , function ( ) {
12
- return this . get ( 'dataTask.lastSuccessful' ) || ! this . get ( 'dataTask.isRunning' ) ;
13
- } ) ,
14
-
15
- dataTask : task ( function * ( ) {
16
- let data = yield ajax ( '/api/v1/summary' ) ;
17
-
18
- addCrates ( this . store , data . new_crates ) ;
19
- addCrates ( this . store , data . most_downloaded ) ;
20
- addCrates ( this . store , data . just_updated ) ;
21
- addCrates ( this . store , data . most_recently_downloaded ) ;
22
-
23
- return data ;
24
- } ) . drop ( ) ,
4
+ hasData : true ,
25
5
} ) ;
26
-
27
- function addCrates ( store , crates ) {
28
- for ( let i = 0 ; i < crates . length ; i ++ ) {
29
- crates [ i ] = store . push ( store . normalize ( 'crate' , crates [ i ] ) ) ;
30
- }
31
- }
Original file line number Diff line number Diff line change 1
1
import Route from '@ember/routing/route' ;
2
+ import { inject as service } from '@ember/service' ;
2
3
3
4
export default Route . extend ( {
5
+ fetcher : service ( ) ,
6
+
4
7
headTags ( ) {
5
8
return [
6
9
{
@@ -13,8 +16,21 @@ export default Route.extend({
13
16
] ;
14
17
} ,
15
18
16
- setupController ( controller ) {
17
- this . controllerFor ( 'application' ) . set ( 'searchQuery' , null ) ;
18
- controller . dataTask . perform ( ) ;
19
+ model ( ) {
20
+ return this . fetcher . ajax ( '/api/v1/summary' ) ;
21
+ } ,
22
+
23
+ // eslint-disable-next-line no-unused-vars
24
+ afterModel ( model , transition ) {
25
+ addCrates ( this . store , model . new_crates ) ;
26
+ addCrates ( this . store , model . most_downloaded ) ;
27
+ addCrates ( this . store , model . just_updated ) ;
28
+ addCrates ( this . store , model . most_recently_downloaded ) ;
19
29
} ,
20
30
} ) ;
31
+
32
+ function addCrates ( store , crates ) {
33
+ for ( let i = 0 ; i < crates . length ; i ++ ) {
34
+ crates [ i ] = store . push ( store . normalize ( 'crate' , crates [ i ] ) ) ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ import Service , { inject as service } from '@ember/service' ;
2
+ import ajax from 'ember-fetch/ajax' ;
3
+
4
+ export default Service . extend ( {
5
+ fastboot : service ( ) ,
6
+
7
+ ajax ( url ) {
8
+ let fastboot = this . fastboot ;
9
+ let shoebox = this . fastboot . shoebox ;
10
+ let cache = shoebox . retrieve ( 'ajax-cache' ) ;
11
+ if ( ! cache ) {
12
+ cache = { } ;
13
+ }
14
+
15
+ if ( cache [ url ] ) {
16
+ return cache [ url ] ;
17
+ }
18
+
19
+ return ajax ( url ) . then ( function ( resp ) {
20
+ if ( shoebox && fastboot . isFastBoot ) {
21
+ cache [ url ] = deepCopy ( resp ) ;
22
+ shoebox . put ( 'ajax-cache' , cache ) ;
23
+ }
24
+ return resp ;
25
+ } ) ;
26
+ } ,
27
+ } ) ;
28
+
29
+ function deepCopy ( obj ) {
30
+ return JSON . parse ( JSON . stringify ( obj ) ) ;
31
+ }
You can’t perform that action at this time.
0 commit comments