mercredi 13 décembre 2017

Properly formatting Ember custom serializer response for sideloaded JSON API data

I want to try serving sideloaded data to my Ember app. I have a city model which hasMany fireStations. I changed my hasMany relationship to have an { async: false } option to coincide with sideloading, since the data will no longer be loaded asynchronously.

I use a custom serializer, and I am logging the response from normalize(). It looks like this for my data.

{  
  "data":{  
      "id":"3",
      "type":"city",
      "attributes":{  
        "name":"Anytown USA"
      },
      "relationships":{  
        "fireStations":{  
            "data":[  
              {  
                  "id":"17",
                  "type":"fire-station"
              },
              {  
                  "id":"18",
                  "type":"fire-station"
              }
            ]
        }
      }
  },
  "included":[  
      {  
        "id":"17",
        "type":"fire-station",
        "attributes":{  
            "name":"North Side Fire Station"
        },
        "relationships":{}
      },
      {  
        "id":"18",
        "type":"fire-station",
        "attributes":{  
            "name":"East Side Fire Station"
        },
        "relationships":{}
      }
  ]
}

I think my sideloaded data is properly formatted. It seems to match the example in the guides. The included array is populated with all my sideloaded data, and it all seems to be formatted as needed.

However, I'm hitting this error in my app.

Assertion Failed: You looked up the 'fireStations' relationship on a 'city' with id 3 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async ('DS.hasMany({ async: true })')

I can't figure out what the issue is here. I forked ember-data locally and I do indeed see this assertion is triggered.

If I manually loop over the manyArray in has-many.js, I see that each record is marked as isEmpty being true

Why are the has-many records returning isEmpty === true? What might I be doing wrong that is preventing sideloading from working correctly?




Aucun commentaire:

Enregistrer un commentaire