jeudi 2 juillet 2015

Property named "type" on embedded records with RESTSerializer in Ember Data - Error no model was found

I use Ember Data 1.13.4.

I have a model with some embedded records, let's say they look like this:

var Partner = DS.Model.extend({
  name: DS.attr(),
  addresses: DS.hasMany('address', { async: false } ),
});

var Address = DS.Model.extend({
  type: DS.attr(),
  zip: DS.attr(),
  city: DS.attr(),
  street: DS.attr()
});

The API sends back the Address records embedded in the Partner records. Example response:

{
  "partners": [
    {
      "id": 47,
      "name": "Johnny",
      "addresses": [
        {
          "id": 7,
          "type": "MAIN",
          "zip": "1234",
          "city": "City-X",
          "street": "6. Nowhere"
        }
      ],
    },
  ]
}

The problem is that type on the Address model is just a normal property, but Ember Data wants it to be the type of the embedded model, and I get this assertion message:

Error: No model was found for 'MAIN' 

Where 'MAIN' is the content of the type property.

I can't change how the API sends back data, I need to adapt to it.

How do I do this?




Aucun commentaire:

Enregistrer un commentaire