lundi 12 octobre 2015

Ember Data Nested Resources Tree Structure

I have a slightly peculiar problem with loading my tree structure into Ember.

My models are:

book.js
  - parts: DS.hasMany('part', {inverse: 'book', async: true})

part.js
  - subparts: DS.hasMany('part', {inverse: 'parent_part', async: true}),

With the following API responses:

GET /api/books:
{
  books: [
    {id: 1, links: {parts: "/api/books/1/parts"}},
    ...
  ]
}

GET /api/books/1/parts:
{
  parts: [
  {
    id: 1,
    subparts: [10, 11]
  },
  {
    id: 2,
    subparts: []
  }
  ]
}

The problem is in the tree nature of the parts: The book only has direct descendants id 1 and 2, but these have sub-parts on their own. The structure as it is works but results in multiple sub-queries for each part that was not included in the /books/1/parts result. I want to avoid these queries, not only because of performance reasons but also because I will need additional query parameters which would get lost at this step... I know about coalesceFindRequests but it introduces new problems.

To rephrase the problem, Ember Data thinks that every part that is included in the /books/1/parts response should be added directly to the book:parts property. How can I still load all records of the parts tree at the same time?

I tried renaming the fields, but Ember Data assigns the records based on the model name, not the field name.

I fear that some creative adapter overriding will be necessary here. Any idea appreciated. The backend is completely under my control, so I could change things on that end, too.




Aucun commentaire:

Enregistrer un commentaire