Skip to content

Commit fa6aeab

Browse files
efxJen Weber
authored andcommitted
ember data section class syntax (ember-learn#543) (ember-learn#553)
* WIP class syntax * fix relationships snippets * mostly convert adapters + serializers * update to runspired's explanation of double extend * update other examples, fix typos * address comments to clean up examples * unify destructuring style
1 parent 976ef34 commit fa6aeab

File tree

8 files changed

+310
-241
lines changed

8 files changed

+310
-241
lines changed

guides/release/models/customizing-adapters.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
In Ember Data, an Adapter determines how data is persisted to a
22
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+
67
Ember Data's default Adapter has some built-in assumptions about
78
how a [REST API should look](http://jsonapi.org/). If your backend conventions
89
differ from those assumptions, Ember Data allows either slight adjustments
@@ -25,10 +26,11 @@ specific Adapters.
2526

2627
```javascript {data-filename=app/adapters/application.js}
2728
import DS from 'ember-data';
29+
const { JSONAPIAdapter } = DS;
2830

29-
export default DS.JSONAPIAdapter.extend({
31+
export default class ApplicationAdapter extends JSONAPIAdapter {
3032
// Application specific overrides go here
31-
});
33+
}
3234
```
3335

3436
If you have one model that has exceptional rules for communicating
@@ -39,10 +41,11 @@ following file:
3941

4042
```javascript {data-filename=app/adapters/post.js}
4143
import DS from 'ember-data';
44+
const { JSONAPIAdapter } = DS;
4245

43-
export default DS.JSONAPIAdapter.extend({
44-
namespace: 'api/v1'
45-
});
46+
export default class ApplicationAdapter extends JSONAPIAdapter {
47+
namespace = 'api/v1';
48+
}
4649
```
4750

4851
Ember Data comes with several built-in adapters.
@@ -148,10 +151,11 @@ specific URL namespace.
148151

149152
```javascript {data-filename=app/adapters/application.js}
150153
import DS from 'ember-data';
154+
const { JSONAPIAdapter } = DS;
151155

152-
export default DS.JSONAPIAdapter.extend({
153-
namespace: 'api/1'
154-
});
156+
export default class ApplicationAdapter extends JSONAPIAdapter {
157+
namespace = 'api/1';
158+
}
155159
```
156160

157161
Requests for `person` would now target `http://emberjs.com/api/1/people/1`.
@@ -165,10 +169,11 @@ property on the adapter.
165169

166170
```javascript {data-filename=app/adapters/application.js}
167171
import DS from 'ember-data';
172+
const { JSONAPIAdapter } = DS;
168173

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+
}
172177
```
173178

174179
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
187192
```javascript {data-filename=app/adapters/application.js}
188193
import DS from 'ember-data';
189194
import { underscore } from '@ember/string';
195+
const { JSONAPIAdapter } = DS;
190196

191-
export default DS.JSONAPIAdapter.extend({
197+
export default class ApplicationAdapter extends JSONAPIAdapter {
192198
pathForType(type) {
193199
return underscore(type);
194200
}
195-
});
201+
}
196202
```
197203

198204
Requests for `person` would now target `/person/1`.
@@ -202,44 +208,46 @@ Requests for `user-profile` would now target `/user_profile/1`.
202208

203209
Some APIs require HTTP headers, e.g. to provide an API key. Arbitrary
204210
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
207213
should not be arrays or objects.)
208214

209215
```javascript {data-filename=app/adapters/application.js}
210216
import DS from 'ember-data';
217+
const { JSONAPIAdapter } = DS;
211218

212-
export default DS.JSONAPIAdapter.extend({
213-
init() {
214-
this._super(...arguments);
219+
export default class ApplicationAdapter extends JSONAPIAdapter {
220+
constructor() {
221+
super(...arguments);
215222

216-
this.set('headers', {
223+
this.headers = {
217224
'API_KEY': 'secret key',
218225
'ANOTHER_HEADER': 'Some header value'
219-
});
226+
};
220227
}
221-
});
228+
}
222229
```
223230

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.
227234

228235
```javascript {data-filename=app/adapters/application.js}
229236
import DS from 'ember-data';
230-
import { computed } from '@ember/object';
237+
import { tracked } from '@glimmer/tracking';
231238
import { inject as service } from '@ember/service';
239+
const { JSONAPIAdapter } = DS;
232240

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() {
237245
return {
238246
'API_KEY': this.session.authToken,
239247
'ANOTHER_HEADER': 'Some header value'
240248
};
241-
})
242-
});
249+
}
250+
}
243251
```
244252

245253
In some cases, your dynamic headers may require data from some
@@ -278,10 +286,11 @@ does not specify an `serializer:application`.
278286

279287
```javascript {data-filename=app/adapters/my-custom-adapter.js}
280288
import DS from 'ember-data';
289+
const { JSONAPIAdapter } = DS;
281290

282-
export default DS.JSONAPIAdapter.extend({
283-
defaultSerializer: '-default'
284-
});
291+
export default class MyCustomAdapter extends JSONAPIAdapter {
292+
defaultSerializer = '-default';
293+
}
285294
```
286295

287296
## Community Adapters

0 commit comments

Comments
 (0)