Gathering detailed insights and metrics for active-model-adapter
Gathering detailed insights and metrics for active-model-adapter
Gathering detailed insights and metrics for active-model-adapter
Gathering detailed insights and metrics for active-model-adapter
@ember-data/rfc395-data
JSON data for Ember.js RFC #395
typeorm
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
@rails/actiontext
Edit and display rich text in Rails applications
parse5-htmlparser2-tree-adapter
htmlparser2 tree adapter for parse5.
Adapters and Serializers for Rails's ActiveModel::Serializers
npm install active-model-adapter
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
102 Stars
358 Commits
54 Forks
17 Watching
5 Branches
32 Contributors
Updated on 04 Mar 2024
TypeScript (82.6%)
JavaScript (14.12%)
HTML (3.15%)
Handlebars (0.13%)
Cumulative downloads
Total Downloads
Last day
-26.4%
2,387
Compared to previous day
Last week
-31.1%
15,995
Compared to previous week
Last month
5.5%
87,172
Compared to previous month
Last year
30.5%
1,544,670
Compared to previous year
69
ember install active-model-adapter
You should make an ApplicationAdapter
if you don't already have one:
1// app/adapters/application.js 2import ActiveModelAdapter from 'active-model-adapter'; 3 4export default class ApplicationAdapter extends ActiveModelAdapter {}
If you need to subclass the ActiveModelSerializer
, you can import it
into your serializer:
1// app/serializers/post.js 2 3import { ActiveModelSerializer } from 'active-model-adapter'; 4 5export default class PostSerializer extends ActiveModelSerializer {}
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
Ruby gem. This Adapter expects specific settings using ActiveModel::Serializers,
embed :ids, embed_in_root: true
which sideloads the records.
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.
Since ActiveModelAdapter 2.1.0 however, you don't need the "_id" or "_ids" suffix on keys for relationships.
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:
1// app/models/famous-person.js 2 3export default class FamousPerson extends Model { 4 @attr() firstName; 5 @attr() lastName; 6 @attr() occupation; 7}
The JSON returned should look like this:
1{ 2 "famous_person": { 3 "id": 1, 4 "first_name": "Barack", 5 "last_name": "Obama", 6 "occupation": "President" 7 } 8}
Let's imagine that Occupation
and Person
are just another model:
1// app/models/person.js 2 3export default class Person extends Model { 4 @attr() firstName; 5 @attr() lastName; 6 @belongsTo('occupation') occupation; 7} 8 9// app/models/occupation.js 10export default class Occupation extends Model { 11 @attr() name; 12 @attr('number') salary; 13 @hasMany('person') people; 14}
The JSON needed to avoid extra server calls, should look like this:
1{ 2 "people": [ 3 { 4 "id": 1, 5 "first_name": "Barack", 6 "last_name": "Obama", 7 "occupation_id": 1 8 } 9 ], 10 "occupations": [ 11 { 12 "id": 1, 13 "name": "President", 14 "salary": 100000, 15 "person_ids": [1] 16 } 17 ] 18}
If your model has polymorphic relationships, the ActiveModelAdapter supports two forms in your response.
When using ActiveModelSerializers in Rails, you can opt into this
payload using the polymorphic: true
option when calling has_many
or
belongs_to
.
1class BookSerializer 2 attributes :id, :name 3 belongs_to :person, polymorphic: true 4end
The first, and preferred format, is to use the name of the relationship as the key and an object with the type and foreign key as the value.
For example, given the following model definitions:
1// app/models/book.js 2export default class Book extends Model { 3 @attr() name; 4 @belongsTo('person', { polymorphic: true }) author; 5} 6 7// app/models/author.js 8export default class Author extends Model { 9 @attr() name; 10 @hasMany('book') books; 11}
The object would look like:
1{ 2 "type": "person", 3 "id": 1 4}
and the full payload would look like this:
1{ 2 "book": { 3 "id": "1", 4 "name": "Yes, Please", 5 "author": { // these are the lines 6 "id": 1, // that define the 7 "type": "person" // polymorphic relationship 8 } 9 }, 10 "people": [{ 11 "id": 1, 12 "name": "Amy Poehler" 13 }] 14}
The second format allows you to specify using two keys in the model's
payload following the format of relationship_name_id
and
relationship_name_type
. This format does not work with hasMany
relationships. This format may also be removed for Ember Data 3.0;
it is currently only supported for legacy reasons.
Using the above model definitions, the single model response would look like this:
1{ 2 "book": { 3 "id": "1", 4 "name": "Yes, Please", 5 "author_id": 1, // these two lines 6 "author_type": "person" // tell Ember Data what the polymorphic 7 // relationship is. 8 } 9}
The full response would be look like this:
1{ 2 "book": { 3 "id": "1", 4 "name": "Yes, Please", 5 "author_id": 1, // these two lines 6 "author_type": "person" // tell Ember Data what the polymorphic 7 // relationship is. 8 }, 9 "people": [{ 10 "id": 1, 11 "name": "Amy Poehler" 12 }] 13}
git clone
this repositoryyarn install
ember test
ember test --server
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.
No vulnerabilities found.
No security vulnerabilities found.