Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bertdeblock/ember-data-record-meta
Record meta management for Ember applications.
https://github.com/bertdeblock/ember-data-record-meta
ember ember-addon ember-data record-meta
Last synced: 17 days ago
JSON representation
Record meta management for Ember applications.
- Host: GitHub
- URL: https://github.com/bertdeblock/ember-data-record-meta
- Owner: bertdeblock
- License: mit
- Created: 2019-08-17T07:32:25.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-11T02:17:36.000Z (almost 2 years ago)
- Last Synced: 2024-10-02T11:50:45.598Z (about 1 month ago)
- Topics: ember, ember-addon, ember-data, record-meta
- Language: JavaScript
- Size: 4.68 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Ember Data Record Meta
[![CI](https://github.com/bertdeblock/ember-data-record-meta/workflows/CI/badge.svg)](https://github.com/bertdeblock/ember-data-record-meta/actions?query=workflow%3ACI)
[![NPM Version](https://badge.fury.io/js/ember-data-record-meta.svg)](https://badge.fury.io/js/ember-data-record-meta)
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)Record meta management for Ember applications.
## Compatibility
- Ember.js v3.12 or above (lower probably works as well, but isn't tested explicitly)
- Ember CLI v2.13 or above
- Node.js v10 or above## Installation
```shell
ember install ember-data-record-meta
```## Usage
### Normalizing and Storing Record Meta
```javascript
// app/serializers/project.jsimport { inject as service } from '@ember/service';
import { camelize } from '@ember/string';
import JSONAPISerializer from '@ember-data/serializer/json-api';export default class ProjectSerializer extends JSONAPISerializer {
@service recordMeta;normalize() {
this.recordMeta.normalizeRecordMeta(...arguments, {
keyTransform: camelize,
});return super.normalize(...arguments);
}
}
```Using the `normalizeRecordMeta` method, we can normalize and store record meta on the `record-meta` service so we can access it later on.
`normalizeRecordMeta` accepts the same arguments as `normalize`, but also an additional options object.
If the meta keys returned by the API do not have the desired format, you can specify a `keyTransform` function.
`keyTransform` receives a meta key as its sole argument and should return that same meta key in the desired format.
In the example above, we use the `camelize` util from `@ember/string` to camelize all meta keys returned by the API.
### Accessing Record Meta
#### Using the `record-meta` Helper
```handlebars
{{! app/templates/project.hbs }}{{#let (record-meta this.project) as |projectMeta|}}
{{#if projectMeta.userCanDelete}}
Delete Project
{{/if}}
{{/let}}
```#### Using the `record-meta` Service
```javascript
// app/controllers/project.jsimport Controller from '@ember/controller';
import { inject as service } from '@ember/service';export default class ProjectController extends Controller {
@service recordMeta;get project() {
return this.model;
}get projectMeta() {
return this.recordMeta.getRecordMeta('project', this.project.id);
}
}
```💡 Working versions of these code examples can be found in [this addon's dummy app](./tests/dummy/app/).
## Contributing
See the [Contributing](CONTRIBUTING.md) guide for details.
## License
This project is licensed under the [MIT License](LICENSE.md).