0
votes

I've got two models of ember-data.

App.Component = DS.Model.extend({
    componentGroup: DS.belongsTo('componentGroup', {async: true}),
    name: DS.attr('string')
});

App.ComponentGroup = DS.Model.extend({
    components: DS.hasMany('component', {async: true}),
    name: DS.attr('string')
});

I am getting list of component_group and then when i click one of them i am trying to fetch all of components this group contains

Default behavior in ember-data beta 9 is to create n+1 get requests for getting that data. If i set

coalesceFindRequests: true

parameter, requests are merged into one but still using that huge array of ids. What i need is to ask API for giving all components that belongs to component_group with id X so rest call could look something like that

/component_group/X/components

It's more efficient when component group have a lot of children.

Is it possible somehow to achieve such behavior ?

1
Why not include the Components in your component group serializer?Patsy Issa
I want to lazy load each of group because amount of data is big. I tried also to load it when trying to fetch only one group (component_group/X) with one group data but it doesn't work while you firstly load all groups (call is not executed because of cache)Dawid Wengrzik

1 Answers

0
votes

Solution is pretty simple but not well documented.

Instead of giving array of ids with component group json you can provide instead links array, filled with field -> link_to_fetch_group

So my final json changed to

{
  "component_group": [
  {
    "id":"4",
    "name":"Klejarnia",
    "links":
      {"components":"/api/rest/component_groups/4/components"}
  }]
}