active-model-adapter
- Version 4.0.1
- Published
- 50.2 kB
- 5 dependencies
- MIT license
Install
npm i active-model-adapter
yarn add active-model-adapter
pnpm add active-model-adapter
Overview
Adapters and Serializers for Rails's ActiveModel::Serializers
Index
Classes
class ActiveModelAdapter
class ActiveModelAdapter extends RESTAdapter {}
The ActiveModelAdapter is a subclass of the RESTAdapter designed to integrate with a JSON API that uses an underscored naming convention instead of camelCasing. It has been designed to work out of the box with the [active\_model\_serializers](http://github.com/rails-api/active_model_serializers) Ruby gem. This Adapter expects specific settings using ActiveModel::Serializers,
embed :ids, embed_in_root: true
which sideloads the records.This adapter extends the DS.RESTAdapter by making consistent use of the camelization, decamelization and pluralization methods to normalize the serialized JSON into a format that is compatible with a conventional Rails backend and Ember Data.
## JSON Structure
The ActiveModelAdapter expects the JSON returned from your server to follow the REST adapter conventions substituting underscored keys for camelcased ones.
Unlike the DS.RESTAdapter, async relationship keys must be the singular form of the relationship name, followed by "_id" for DS.belongsTo relationships, or "_ids" for DS.hasMany relationships.
### Conventional Names
Attribute names in your JSON payload should be the underscored versions of the attributes in your Ember.js models.
For example, if you have a
Person
model:export default class FamousPerson extends Model {@attr() firstName;@attr() lastName;@attr() occupation;}The JSON returned should look like this:
{"famous_person": {"id": 1,"first_name": "Barack","last_name": "Obama","occupation": "President"}}Let's imagine that
Occupation
is just another model:export default class Person extends Model {@attr() firstName;@attr() lastName;@belongsTo('occupation') occupation;}export default class Occupation extends Model {@attr() name;@attr('number') salary;@hasMany('person') people;}The JSON needed to avoid extra server calls, should look like this:
{"people": [{"id": 1,"first_name": "Barack","last_name": "Obama","occupation_id": 1}],"occupations": [{"id": 1,"name": "President","salary": 100000,"person_ids": [1]}]}ActiveModelAdapter DS DS.RESTAdapter
property defaultSerializer
defaultSerializer: string;
method handleResponse
handleResponse: ( status: number, headers: AnyObject, payload: ActiveModelPayload, requestData: AnyObject | AdapterError) => any;
The ActiveModelAdapter overrides the
handleResponse
method to format errors passed to a DS.InvalidError for all 422 Unprocessable Entity responses.A 422 HTTP response from the server generally implies that the request was well formed but the API was unable to process it because the content was not semantically correct or meaningful per the API.
For more information on 422 HTTP Error code see 11.2 WebDAV RFC 4918 https://tools.ietf.org/html/rfc4918#section-11.2
handleResponse
Parameter status
Parameter headers
Parameter payload
{Object | AdapterError} response
method pathForType
pathForType: <K extends string | number | symbol>(modelName: K) => string;
The ActiveModelAdapter overrides the
pathForType
method to build underscored URLs by decamelizing and pluralizing the object type name.this.pathForType("famousPerson");//=> "famous_people"pathForType
Parameter modelName
String
Package Files (1)
Dependencies (5)
Dev Dependencies (69)
- @ember/optional-features
- @ember/test-helpers
- @embroider/test-setup
- @glimmer/component
- @glimmer/tracking
- @types/ember
- @types/ember__application
- @types/ember__array
- @types/ember__component
- @types/ember__controller
- @types/ember__debug
- @types/ember__engine
- @types/ember__error
- @types/ember__object
- @types/ember__polyfills
- @types/ember__routing
- @types/ember__runloop
- @types/ember__service
- @types/ember__string
- @types/ember__template
- @types/ember__test
- @types/ember__test-helpers
- @types/ember__utils
- @types/ember-data
- @types/ember-data__adapter
- @types/ember-data__model
- @types/ember-data__serializer
- @types/ember-data__store
- @types/ember-qunit
- @types/ember-resolver
- @types/htmlbars-inline-precompile
- @types/qunit
- @types/rsvp
- @typescript-eslint/eslint-plugin
- @typescript-eslint/parser
- babel-eslint
- broccoli-asset-rev
- ember-cli
- ember-cli-dependency-checker
- ember-cli-inject-live-reload
- ember-cli-mirage
- ember-cli-sri
- ember-cli-terser
- ember-data
- ember-disable-prototype-extensions
- ember-export-application-global
- ember-load-initializers
- ember-maybe-import-regenerator
- ember-qunit
- ember-resolver
- ember-source
- ember-source-channel-url
- ember-try
- eslint
- eslint-config-prettier
- eslint-plugin-ember
- eslint-plugin-node
- eslint-plugin-prettier
- eslint-plugin-qunit
- loader.js
- npm-run-all
- pretender
- prettier
- qunit
- qunit-dom
- release-it
- release-it-lerna-changelog
- typescript
- webpack
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/active-model-adapter
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/active-model-adapter)
- HTML<a href="https://www.jsdocs.io/package/active-model-adapter"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3161 ms. - Missing or incorrect documentation? Open an issue for this package.