File tree Expand file tree Collapse file tree 3 files changed +50
-30
lines changed Expand file tree Collapse file tree 3 files changed +50
-30
lines changed Original file line number Diff line number Diff line change 11import  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' ; 
72
83export  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 , 
255} ) ; 
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 11import  Route  from  '@ember/routing/route' ; 
2+ import  {  inject  as  service  }  from  '@ember/service' ; 
23
34export  default  Route . extend ( { 
5+   fetcher : service ( ) , 
6+ 
47  headTags ( )  { 
58    return  [ 
69      { 
@@ -13,8 +16,20 @@ export default Route.extend({
1316    ] ; 
1417  } , 
1518
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+   afterModel ( model ,  transition )  { 
24+     addCrates ( this . store ,  model . new_crates ) ; 
25+     addCrates ( this . store ,  model . most_downloaded ) ; 
26+     addCrates ( this . store ,  model . just_updated ) ; 
27+     addCrates ( this . store ,  model . most_recently_downloaded ) ; 
1928  } , 
2029} ) ; 
30+ 
31+ function  addCrates ( store ,  crates )  { 
32+   for  ( let  i  =  0 ;  i  <  crates . length ;  i ++ )  { 
33+     crates [ i ]  =  store . push ( store . normalize ( 'crate' ,  crates [ i ] ) ) ; 
34+   } 
35+ } 
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