mardi 2 juin 2015

Ember-data model relationships with array

I am having a hard time to understand how the model relationships works in ember-data. I understand the concept one to many or one to one or many to many, but I don't understand how to use it properly..

My API is sending me this data :

{
    array1: [
        {
          commonID: '23',
          otherData: '',
          [...],
          array2: [
              {
                   commonID: '23',
                   someData: '',
                   [...]   
              },
              {
                   commonID: '23',
                   someData: '',
                   [...]   
              }
          ]
        },
        {
          commonID: '24',
          otherData: '',
          [...],
          array2: [
              {
                   commonID: '24',
                   someData: '',
                   [...]   
              }
          ]
        }
    ]
}

As you can see I receive an array that contain some data and an other array.

I don't really know if how I should create my models, should I have just one model ? or multiple like this ? (correct me if its wrong) :

//array1.js
import DS from "ember-data";

export default DS.Model.extend({
    commonID:         DS.attr('string'),
    otherData:        DS.attr('string'),
    array2:           DS.hasMany('fixture')
});

//array2.js
import DS from "ember-data";

export default DS.Model.extend({
    commonID:         DS.attr('string'),
    otherData:        DS.attr('string'),
    array1:           DS.belongsTo('fixture')
});

I would like to be able to save my arrays in the store and keep the relationships between them. If I do a this.store.find('array1', {commonID: '23'} ); I would like to get also all of the array2 that are related to array1. (the commonID would be the same if they are related).

Do I have to create a custom serializer ?

So many questions, thanks for you help !




Aucun commentaire:

Enregistrer un commentaire