🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

ember-data-updating-json-api-relationships

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-data-updating-json-api-relationships

Updating JSON:API relationships independently in Ember Data

0.0.4
latest
Source
npm
Version published
Weekly downloads
9
350%
Maintainers
1
Weekly downloads
 
Created
Source

Ember Observer Score Build Status

ember-data-updating-json-api-relationships

This addon adds support to Ember Data to update relationships independently. See Updating Relationships in the JSON:API spec for more details. This library works for updating to-one and to-many relationships, as described in the JSON:API spec.

Installation

ember install ember-data-updating-json-api-relationships

Getting Started

First, extend the adapter:

// app/adapters/application.js
import DS from 'ember-data';
import JSONAPIAdapter from 'ember-data-updating-json-api-relationships/adapters/adapter';

export default JSONAPIAdapter.extend({
});

Second, extend the serializer:

// app/serializers/application.js
import DS from 'ember-data';
import JSONAPISerializer from 'ember-data-updating-json-api-relationships/serializers/serializer';

export default JSONAPISerializer.extend({
});

Third, add the model mixin to the model with the relationship that you want to update independently:

// app/models/article.js
import DS from 'ember-data';
import ModelMixin from 'ember-data-updating-json-api-relationships/mixins/model';

let { Model, attr, hasMany, belongsTo } = DS;

export default Model.extend(ModelMixin, {
  title: attr('string'),
  tags: hasMany(),
  author: belongsTo()
});

Lastly, call updateRelationship(relationship) on your model:

article.get('tags').pushObject(tag1);
article.get('tags').pushObject(tag2);

// PATCH /articles/:id/relationships/tags
article.updateRelationship('tags');

article.set('author', author);

// PATCH /articles/:id/relationships/author
article.updateRelationship('author');

Customizations

Customizing Relationship URLs

This addon will set relationship URLs to what is recommended in the spec, so something like /articles/:id/relationships/tags for a to-many relationship and /articles/:id/relationships/author for a to-one relationship. If you need to override this, use the urlForUpdateRelationship() hook:

// app/adapters/article.js
export default ApplicationAdapter.extend({
  urlForUpdateRelationship(id, modelName, snapshot, relationshipToUpdate) {
    let { host, namespace } = this;
    if (relationshipToUpdate === 'tags') {
      return `${host}/${namespace}/articles/${id}/categories`;
    }
  }
})

Keywords

ember-addon

FAQs

Package last updated on 25 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts