1
1
In Ember Data, an Adapter determines how data is persisted to a
2
2
backend data store. Things such as the backend host, URL format
3
- and headers used to talk to a REST API can all be configured
4
- in an adapter.
5
-
3
+ and headers used to talk to a REST API can all be configured
4
+ in an adapter. You can even switch to storing data in local storage
5
+ using a [ local storage adapter] ( https://github.com/locks/ember-localstorage-adapter ) .
6
+
6
7
Ember Data's default Adapter has some built-in assumptions about
7
8
how a [ REST API should look] ( http://jsonapi.org/ ) . If your backend conventions
8
9
differ from those assumptions, Ember Data allows either slight adjustments
@@ -25,10 +26,11 @@ specific Adapters.
25
26
26
27
``` javascript {data-filename=app/adapters/application.js}
27
28
import DS from ' ember-data' ;
29
+ const { JSONAPIAdapter } = DS ;
28
30
29
- export default DS . JSONAPIAdapter . extend ( {
31
+ export default class ApplicationAdapter extends JSONAPIAdapter {
30
32
// Application specific overrides go here
31
- });
33
+ }
32
34
```
33
35
34
36
If you have one model that has exceptional rules for communicating
@@ -39,10 +41,11 @@ following file:
39
41
40
42
``` javascript {data-filename=app/adapters/post.js}
41
43
import DS from ' ember-data' ;
44
+ const { JSONAPIAdapter } = DS ;
42
45
43
- export default DS . JSONAPIAdapter . extend ( {
44
- namespace: ' api/v1'
45
- });
46
+ export default class ApplicationAdapter extends JSONAPIAdapter {
47
+ namespace = ' api/v1' ;
48
+ }
46
49
```
47
50
48
51
Ember Data comes with several built-in adapters.
@@ -148,10 +151,11 @@ specific URL namespace.
148
151
149
152
``` javascript {data-filename=app/adapters/application.js}
150
153
import DS from ' ember-data' ;
154
+ const { JSONAPIAdapter } = DS ;
151
155
152
- export default DS . JSONAPIAdapter . extend ( {
153
- namespace: ' api/1'
154
- });
156
+ export default class ApplicationAdapter extends JSONAPIAdapter {
157
+ namespace = ' api/1' ;
158
+ }
155
159
```
156
160
157
161
Requests for ` person ` would now target ` http://emberjs.com/api/1/people/1 ` .
@@ -165,10 +169,11 @@ property on the adapter.
165
169
166
170
``` javascript {data-filename=app/adapters/application.js}
167
171
import DS from ' ember-data' ;
172
+ const { JSONAPIAdapter } = DS ;
168
173
169
- export default DS . JSONAPIAdapter . extend ( {
170
- host: ' https://api.example.com'
171
- });
174
+ export default class ApplicationAdapter extends JSONAPIAdapter {
175
+ host = ' https://api.example.com' ;
176
+ }
172
177
```
173
178
174
179
Requests for ` person ` would now target ` https://api.example.com/people/1 ` .
@@ -187,12 +192,13 @@ underscore_case instead of dash-case you could override the
187
192
``` javascript {data-filename=app/adapters/application.js}
188
193
import DS from ' ember-data' ;
189
194
import { underscore } from ' @ember/string' ;
195
+ const { JSONAPIAdapter } = DS ;
190
196
191
- export default DS . JSONAPIAdapter . extend ( {
197
+ export default class ApplicationAdapter extends JSONAPIAdapter {
192
198
pathForType (type ) {
193
199
return underscore (type);
194
200
}
195
- });
201
+ }
196
202
```
197
203
198
204
Requests for ` person ` would now target ` /person/1 ` .
@@ -202,44 +208,46 @@ Requests for `user-profile` would now target `/user_profile/1`.
202
208
203
209
Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
204
210
headers can be set as key/value pairs on the ` JSONAPIAdapter ` 's ` headers `
205
- object and Ember Data will send them along with each Ajax request.
206
- (Note that we set headers in ` init ()` because default property values
211
+ object and Ember Data will send them along with each ajax request.
212
+ (Note that we set headers in ` constructor ()` because default property values
207
213
should not be arrays or objects.)
208
214
209
215
``` javascript {data-filename=app/adapters/application.js}
210
216
import DS from ' ember-data' ;
217
+ const { JSONAPIAdapter } = DS ;
211
218
212
- export default DS . JSONAPIAdapter . extend ( {
213
- init () {
214
- this . _super (... arguments );
219
+ export default class ApplicationAdapter extends JSONAPIAdapter {
220
+ constructor () {
221
+ super (... arguments );
215
222
216
- this .set ( ' headers' , {
223
+ this .headers = {
217
224
' API_KEY' : ' secret key' ,
218
225
' ANOTHER_HEADER' : ' Some header value'
219
- }) ;
226
+ };
220
227
}
221
- });
228
+ }
222
229
```
223
230
224
- ` headers ` can also be used as a computed property to support dynamic
225
- headers. In the example below, the headers are generated with a computed
226
- property dependent on the ` session ` service.
231
+ You can combine tracked properties with ES6 getters to make ` headers ` dynamic.
232
+ In the example below, the headers are generated dynamically using a
233
+ property from the ` session ` service.
227
234
228
235
``` javascript {data-filename=app/adapters/application.js}
229
236
import DS from ' ember-data' ;
230
- import { computed } from ' @ember/object ' ;
237
+ import { tracked } from ' @glimmer/tracking ' ;
231
238
import { inject as service } from ' @ember/service' ;
239
+ const { JSONAPIAdapter } = DS ;
232
240
233
-
234
- export default DS . JSONAPIAdapter . extend ({
235
- session : service ( ' session' ),
236
- headers : computed ( ' session.authToken ' , function () {
241
+ export default class ApplicationAdapter extends JSONAPIAdapter {
242
+ @service session;
243
+ @tracked session . authToken ;
244
+ get headers () {
237
245
return {
238
246
' API_KEY' : this .session .authToken ,
239
247
' ANOTHER_HEADER' : ' Some header value'
240
248
};
241
- })
242
- });
249
+ }
250
+ }
243
251
```
244
252
245
253
In some cases, your dynamic headers may require data from some
@@ -278,10 +286,11 @@ does not specify an `serializer:application`.
278
286
279
287
``` javascript {data-filename=app/adapters/my-custom-adapter.js}
280
288
import DS from ' ember-data' ;
289
+ const { JSONAPIAdapter } = DS ;
281
290
282
- export default DS . JSONAPIAdapter . extend ( {
283
- defaultSerializer: ' -default'
284
- });
291
+ export default class MyCustomAdapter extends JSONAPIAdapter {
292
+ defaultSerializer = ' -default' ;
293
+ }
285
294
```
286
295
287
296
## Community Adapters
0 commit comments