lundi 14 août 2017

Ember RESTSerializer, relationships not behaving as expected

I'm using both the RESTAdapter, and RESTSerializer to parse a json payload with relationships (sideloaded). The docs for DS.RESTAdapter state that the sideloaded related data should be returned under a key of their own type, e.g

{
  "posts": {
    "id": 5,
    "title": "I'm Running to Reform the W3C's Tag",
    "author": "Yehuda Katz",
    "comments": [1, 2]
  },
  "comments": [{
    "id": 1,
    "author": "User 1",
    "message": "First!",
  }, {
    "id": 2,
    "author": "User 2",
    "message": "Good Luck!",
  }]
}

When using my own payload, unless I add a type field to each related item, I get a parse error: 'Cannot read property 'replace' of undefined'.

This is my payload:

{
    "practice-items": [
        {
            id: 1,
            title: 'Major Scales',
            discipline: 1
        },
        {
            id: 2,
            title: 'Minor Scales',
            total_issues_count: 5,
            discipline: 1
        }
    ],
    "disciplines": [
        {
            id: 1,
            title: 'Scales',
            type: 'discipline'
        },
        {
            id: 2,
            title: 'Pieces',
            type: 'discipline'
        }
    ]
}

This works - but not if I omit the 'type' field in each related record. Any idea why this is - am I missing something obvious?




Aucun commentaire:

Enregistrer un commentaire