Skip to content

Commit

Permalink
RFC-0522 Ember-Data Deprecate default serializers and adapters (#436)
Browse files Browse the repository at this point in the history
RFC-0522 Ember-Data Deprecate default serializers and adapters
  • Loading branch information
rwjblue authored Sep 18, 2019
2 parents 08e56ef + e889c53 commit 699566a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
29 changes: 29 additions & 0 deletions content/ember-data/v3/default-adapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
id: ember-data:default-adapter
title: Default adapter usage
until: '4.0.0'
since: '3.12.0'
---
## Deprecates both store.defaultAdapter (which defaults to -json-api) and the -json-api adapter fallback behavior

Previously, applications could define the store.adapter property which would be used by defaultAdapter and adapterFor as a fallback for when an adapter was not found by an exact name match.

[RFC 522](https://github.com/emberjs/rfcs/pull/522) Deprecated specifying and using this property in favor of an explicit application adapter fallback.

If you were not setting this value previously, the following should be sufficient to resolve this deprecation:

create the file app/adapters/application.js with the following:

```js
export { default } from '@ember-data/adapters/json-api';
```


If you were setting the adapter property previously to `<adapter-name>`, create the file app/adapters/application.js with the following:

```js
export { default } from './<adapter-name>';
```


More information about custom adapters can be found on the [ember.js/guides](https://guides.emberjs.com/release/models/customizing-adapters/) and in the [API DOCs](https://api.emberjs.com/ember-data/release/modules/@ember-data%2Fadapter)
35 changes: 35 additions & 0 deletions content/ember-data/v3/default-serializer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
id: ember-data:default-serializers
title: Default serializers usage
until: '4.0.0'
since: '3.12.0'
---
## Default serializer deprecations
### deprecate adapter.defaultSerializer fallback
Previously, if no application or type-specific serializer was specified, the store would attempt to lookup a serializer via the `defaultSerializer` property on the type's adapter. This behavior is deprecated in favor of explicitly defining a type-specific serializer or application serializer as described in "clearing these deprecations" below.

### -default serializer fallback in store.serializerFor
Previously, when no type-specific serializer, application serializer, or adapter defaultSerializer had been defined by the app, the `-default` serializer would be used which defaulted to `-json-api`. This behavior is deprecated in favor of explicitly defining an application or type-specific serializer as described below.

## clearing these deprecations
More information about custom serializers can be found in the [Serializer API Docs](https://api.emberjs.com/ember-data/release/modules/@ember-data%2Fserializer) or on the [ember.js/guides](https://guides.emberjs.com/release/models/customizing-serializers/#toc_customizing-serializers)

If a specific model type requires custom serialization, a type-specific serializer can be created. A single `application` serializer can be used a for any model types not requiring custom serialization. To define a type-specific serializer, create an `app/serializers/[type].js` with the following:

```js
import { JSONAPISerializer } from '@ember-data/serializers';

export default class UserSerializer extends JSONApiSerializer {
/*custom code*/
}
```

Defining a serializer for the entire application can be done by adding the file `app/serializers/application.js` with the following:

```js
export { JSONAPISerializer } from '@ember-data/serializers';
```




50 changes: 50 additions & 0 deletions content/ember-data/v3/toJSON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
id: record.toJSON
title: Record toJSON usage
until: '4.0.0'
since: '3.12.0'
---
## Deprecates the built in `record.toJSON`
Previously users could use [`record.toJSON`](https://github.com/emberjs/data/blob/1be481a4924b2b4316c1cc151a58328c88903dcd/packages/store/addon/-private/system/model/model.js#L620) to get a simple JSON serialization of a record instance by either calling the method directly or using `JSON.stringify(record)`.

This method used the now deprecated `-json` serializer to create this JSON representation of the record instead of the user supplied serializer. In addition to the surprising use of a different serializer, this creates an unnecessary dependency on the `JSONSerializer` for applications that may not otherwise have imported and uses this serializer.

We have deprecated EmberData's own implementation of this method in favor of users implementing their own (or refactoring away).

To clear this deprecation users may call record.serialize() or implement their own toJSON instead. The simplest 1:1 refactor is to import a serializer and define a `toJSON` method that returns the serialized data from the model, but users may want to consider implementing a custom "serialize" method that outputs relevant data.

An example of the simple refactor is below:
### before

```js
//app/models/post.js
import Model from '@ember-data/model';

export default class Post extends Model {};

//other app code
const record = store.peekRecord('post');
// users the default serializer, will have a deprecation warning
const output = record.toJSON();
```

### after
```js
//app/models/post.js
import Model from '@ember-data/model';
import { JSONAPISerializer } from '@ember-data/serializers';

export default class Post extends Model {
toJSON(options) {
/* Create a JSON object with relevant data by either:
- iterating the attributes / relationships of the record into a POJO
- calling this.serialize and then munge output into the desired shape
*/
}
};

//other app code
const record = store.peekRecord('post');
// users the default serializer
const output = record.toJSON();
```

0 comments on commit 699566a

Please sign in to comment.