vendredi 3 novembre 2017

Relationships in include with JSON API and Ember Data

Consider the relationship:

Office ->hasMany rooms -> hasMany tables -> hasMany drawers

represented in JSONAPI format as below:

Consider the following relationship:

Offic ->hasMany rooms -> hasMany tables --> hasMany drawers

represented by the following JSON:

  "data": [{
    "type": "office",
    "id": 201,
    "attributes": {
      "owner": 5,
       ......
      "total-rooms": 1,

    },
    "links": {
      "self": "/offices/201",
    },
    "relationships": {

      "rooms": {
        "links": {
          "self": "/offices/201/relationships/rooms",
          "related": "/offices/201/rooms"

        },
        "data":[
          { "type": "room", "id": 201 },

        ]
      }
    }
  }],
  "included": [ {
    "type":"room",
    "id": 201,
    "attributes": {
      "office": 5,
      "some-property": "Test Property tpo see if it works"
    },
    "links": {
      "self": "/offices/5/rooms/201",

    },
    "relationships": {
      "tables": {
        "links": {
          "self": "/offices/5/rooms/201/relationships/rooms",
          "related": "/offices/5/rooms/201/tables",

        },
        "data":[
          { "type": "tables", "id": 801 },

        ]
      }
    }

  },{
      "type":"tables",
      "id": 801,
      "attributes": {
        "room": 201,
        "table-test-data":"Some test data",


      },
      "links": {
        "self": "/offices/5/rooms/201/tables/801",
      },
      "relationships": {
      "drawers": {
        "links": {
          "self": "/offices/5/rooms/201/tables/810/relationships/drawers",
          "related": "/properties/5/rooms/201/tables/810/drawers",

        },
        "data":[
          { "type": "drawer", "id": 1001 },

        ]
      }
    }

    },


  ]

}

I'm trying to model them in EmberJS/Ember data using the following models:

//Office
export default DS.Model.extend({
    totalRooms: DS.attr('number');
    rooms: DS.hasMany('room');
});

//Rooms
export default DS.Model.extend({
    total: DS.attr('number');
    tables: DS.hasMany('table');
});

//Table
export default DS.Model.extend({
    totalTables: DS.attr('number');
    drawers: DS.hasMany('drawer');
});
//Drawer
export default DS.Model.extend({
    totalDrawers: DS.attr('number');
    drawers: DS.attr('string')
});

However, Ember data only loads the primary data's first level relationship. That is, in my controller, when I do the following:

model.offices.forEach(function(office) {
    office.rooms.forEach(function(room){
        //Can access room properties

        // cannot access tables
        console.log(room.tables); //prints a class instead of object

    })

})

I can access all the data for rooms, but when I try and go one level deeper, e.g. tables it comes through as undefined. I don't want to use multiple calls to load data and was wondering if I'm missing something in my JSON returned by the server or I need to handle it differently?

Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire