mercredi 19 février 2020

Ember data - use property other than id for hasMany relationship

I have a many-to-many relationship defined between my tag and payment models as shown below.

//models/tag.js
import Model, { attr, hasMany } from '@ember-data/model';

export default Model.extend({
  name: attr('string'),
  backgroundColour: attr('string'),
  textColour: attr('string'),
  payments: hasMany('payment')
});
// models/payment.js
import Model, { attr, hasMany } from '@ember-data/model';

export default Model.extend({
  date: attr('date'),
  amount: attr('number'),
  paymentId: attr('string'),
  tags: hasMany('tag'),
});

By default, when I add tags to payments, the id of the payment is used as the key for the relationship. My aim is for Ember data to use the paymentId property as the key for this relationship instead.

The snippet below shows the structure of the data I'm loading, where a tag references payments by the paymentId property.

    // Example tag
    {
      "id": "25",
      "name": "Groceries",
      "backgroundColour": "31b04b",
      "textColour": "ffffff",
      "payments": ["20190121201902210"] // References paymentId rather than id
    },
    
    // Example payment
    {
      "id": "1"
      "date": "2019-01-27T22:00:00.000Z",
      "amount": 1644.44,
      "paymentId": "20190121201902210",
      "tags": ["25"]
    }

I've tried to customise the payment serializer as below,

// serializers/payment.js

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
  keyForRelationship(key, _relationship) {
    if (key === 'payments') {
      return 'paymentId';
    }
  },
});

However, when the models are loaded I get this error: Assertion Failed: All elements of a hasMany relationship must be instances of Model, you passed [ "20190121201902210" ].

How can I make Ember data use paymentId rather than id when looking up related payments?




Aucun commentaire:

Enregistrer un commentaire