File tree 2 files changed +40
-11
lines changed
2 files changed +40
-11
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,24 @@ import { computed } from '@ember/object';
4
4
5
5
export default RESTAdapter . extend ( {
6
6
fastboot : service ( ) ,
7
+ fetcher : service ( ) ,
7
8
8
9
namespace : 'api/v1' ,
9
10
11
+ ajax : function ( url , type , options ) {
12
+ if ( type === 'GET' ) {
13
+ let cache = this . fetcher . get ( url ) ;
14
+ if ( cache ) {
15
+ return cache ;
16
+ }
17
+ }
18
+
19
+ return this . _super ( url , type , options ) . then ( resp => {
20
+ this . fetcher . put ( url , resp ) ;
21
+ return resp ;
22
+ } ) ;
23
+ } ,
24
+
10
25
headers : computed ( 'fastboot.{isFastBoot,request.headers}' , function ( ) {
11
26
if ( this . fastboot . isFastBoot ) {
12
27
return { 'User-Agent' : this . fastboot . request . headers . get ( 'User-Agent' ) } ;
Original file line number Diff line number Diff line change 1
1
import Service , { inject as service } from '@ember/service' ;
2
2
import ajax from 'ember-fetch/ajax' ;
3
3
4
+ const KEY = 'ajax-cache' ;
5
+
4
6
export default Service . extend ( {
5
7
fastboot : service ( ) ,
6
8
7
- ajax ( url ) {
9
+ get ( url ) {
10
+ let shoebox = this . fastboot . shoebox ;
11
+ if ( ! shoebox ) {
12
+ return ;
13
+ }
14
+ let cache = shoebox . retrieve ( KEY ) || { } ;
15
+ return cache [ url ] ;
16
+ } ,
17
+
18
+ put ( url , obj ) {
8
19
let fastboot = this . fastboot ;
9
20
let shoebox = this . fastboot . shoebox ;
10
- let cache = shoebox . retrieve ( 'ajax-cache' ) ;
11
- if ( ! cache ) {
12
- cache = { } ;
21
+ if ( ! ( shoebox && fastboot . isFastBoot ) ) {
22
+ return ;
13
23
}
14
24
15
- if ( cache [ url ] ) {
16
- return cache [ url ] ;
25
+ let cache = shoebox . retrieve ( KEY ) || { } ;
26
+ cache [ url ] = deepCopy ( obj ) ;
27
+ shoebox . put ( KEY , cache ) ;
28
+ } ,
29
+
30
+ ajax ( url ) {
31
+ let resp = this . get ( url ) ;
32
+ if ( resp ) {
33
+ return resp ;
17
34
}
18
35
19
- return ajax ( url ) . then ( function ( resp ) {
20
- if ( shoebox && fastboot . isFastBoot ) {
21
- cache [ url ] = deepCopy ( resp ) ;
22
- shoebox . put ( 'ajax-cache' , cache ) ;
23
- }
36
+ return ajax ( url ) . then ( resp => {
37
+ this . put ( url , resp ) ;
24
38
return resp ;
25
39
} ) ;
26
40
} ,
You can’t perform that action at this time.
0 commit comments