@bagaaravel/ember-data-extensions
v2.0.0
Published
Ember Data extensions to communicate with the Bagaaravel JSON API implementation.
Downloads
29
Maintainers
Readme
Bagaaravel Ember Data Extensions
Ember Data extensions to communicate with the Bagaaravel JSON API implementation.
Table of Contents
Introduction
@bagaaravel/ember-data-extensions
is an addon that allows you to easily communicate with the Bagaaravel JSON API implementation.
Support
@bagaaravel/ember-data-extensions
supports Ember v3.24 and up.
Installation
ember install @bagaaravel/ember-data-extensions
Usage
1. Extend the Application Serializer
// app/serializers/application.js
import {
keyForAttribute,
keyForRelationship,
payloadKeyFromModelName,
serialize,
shouldSerializeHasMany
} from '@bagaaravel/ember-data-extensions/serializer'
import JSONAPISerializer from '@ember-data/serializer/json-api'
export default class ApplicationSerializer extends JSONAPISerializer {
// Make sure attribute keys have the correct casing:
keyForAttribute () {
return keyForAttribute(...arguments)
}
// Make sure relationship keys have the correct casing:
keyForRelationship () {
return keyForRelationship(...arguments)
}
// Make sure model types have the correct casing:
payloadKeyFromModelName () {
return payloadKeyFromModelName(...arguments)
}
// Make sure relationships are correctly serialized:
serialize () {
const serialized = super.serialize(...arguments)
return serialize(serialized, ...arguments)
}
// Check when 'hasMany' relationships should be serialized:
shouldSerializeHasMany () {
const superCheck = super.shouldSerializeHasMany(...arguments)
return shouldSerializeHasMany(superCheck, ...arguments)
}
}
2. Extend the Application Adapter
// app/adapters/application.js
import { urlForUpdateRecord } from '@bagaaravel/ember-data-extensions/adapter'
import JSONAPIAdapter from '@ember-data/adapter/json-api'
export default class ApplicationAdapter extends JSONAPIAdapter {
// Make sure the correct URL is used when only saving a relationship:
urlForUpdateRecord () {
const baseUrl = super.urlForUpdateRecord(...arguments)
return urlForUpdateRecord(baseUrl, ...arguments)
}
}
3. Updating Relationships
import {
saveRelationship,
saveRelationships
} from '@bagaaravel/ember-data-extensions/model'
const user = await this.store.findRecord('user', '1')
// Update the user's projects:
saveRelationship(user, 'projects')
// Update the user's company:
saveRelationship(user, 'company')
// Update the user's projects and company:
saveRelationships(user, ['projects', 'company'])
Contributing
See the Contributing guide for details.
License
This project is licensed under the MIT License.
Maintenance
@bagaaravel/ember-data-extensions
is built and maintained by Bagaar.