vendredi 21 août 2015

Ember data JSONAPI complex attribute data

I have a data structure as follows coming back from the server for some filter functionality I'm writing. Where each filter-group has many filters.

data: [
    {
        type: "filter-group",
        id: "556d7f5fa1f9de08500ef4e8_1",
        attributes: {
            name: "Colour",
            created-date: "0001-01-01T00:00:00Z",
            active: true,
            filters: [
                {
                    id: "556d7f5fa1f9de08500ef4e8_1_1",
                    name: "Red",
                    created-date: "0001-01-01T00:00:00Z",
                    active: true
                },
                {
                    id: "556d7f5fa1f9de08500ef4e8_1_2",
                    name: "Blue",
                    created-date: "0001-01-01T00:00:00Z",
                    active: true
                },
                {
                    id: "556d7f5fa1f9de08500ef4e8_1_3",
                    name: "Green",
                    created-date: "0001-01-01T00:00:00Z",
                    active: true
                }
            ]
        }
    }
]

And I have models set up as such:

// models/filter-group.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  active: DS.attr('boolean'),
  client: DS.belongsTo('client', { embedded: 'always' }),
  filters: DS.hasMany('filter', { embedded: 'always' })
});

And:

// models/filter.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  active: DS.attr('boolean'),
  createdDate: DS.attr('date'),
  filterGroup: DS.belongsTo('filter-group', { embedded: 'always' })
});

I'm new to working with JSONAPI, so I'm not sure if my data setup is the right way of going about this. I'm trying to loop through the filter-groups and then inside each, loop through its available filters, using the following handlebars template:

{{#each filterGroups as |filterGroup|}}
    <h6>{{filterGroup.name}}</h6>

    {{#each filterGroup.filters as |filter|}}
        -- Filter output here --
    {{/each}}
{{/each}}

But each filterGroup.filters object is empty. What am I doing wrong here? Am I completely misunderstanding the way the JSONAPISerializer works on structures like this?




Aucun commentaire:

Enregistrer un commentaire