jeudi 6 octobre 2016

Access hasMany/belongsTo relationship data with JSONAPI

Can you help me, please?

I have a model Channel, a model Feature, a model ChannelApplication and a model ApplicationFeature. And I defined them like this:

My model channel.js:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';

export default Model.extend({
  name: attr('string'),
  key: attr('string'),
  multiplePages: attr('boolean'),
  features: hasMany('feature', { async: true })
});

My model feature.js:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';

export default Model.extend({
  name: attr('string'),
  key: attr('string'),
  applicationFeatures: hasMany('application-feature', { async: true }),
  channels: hasMany('channel', { async: true })
});

My model channel-application.js:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';

export default Model.extend({
  name: attr('string'),
  clientId: attr('string'),
  channel: belongsTo('channel'),
  applicationFeatures: hasMany('application-feature', { async: true })
});

My model application-feature.js

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';

export default Model.extend({
  scope: attr('string'),
  connectType: attr('string'),
  channelApplication: belongsTo('channel-application', { async: true }),
  feature: belongsTo('feature', { async: true })
});

I'm using JSONAPI. And in my route I do this:

model() {
    return Ember.RSVP.hash({
      profiles: this.store.findAll('profile'),
      channels: this.store.findAll('channel'),
      channelApplications: this.store.query('channel-application', { filter: { feature: 'inbox'} })
   })
}

So, in my template I need to get my ChannelApplications and for each one I need to know Channel related with this ChannelApplications and know each ApplicationFeature.


    

    // I think in something like this, but this doesn't work of course
    
       <span></span>
       <span></span>
       
          <span></span>
          <span></span>
        
    



channelApplication.applicationFeatures and channelApplication.channels returns nothing.

When I print it return 0.

When I print it prints a <DS.PromiseManyArray>

JSONAPI return for channelApplications: this.store.query('channel-application', { filter: { feature: 'inbox'} }):

{
   "data":[
      {
         "type":"channel-applications",
         "id":"2",
         "attributes":{
            "name":"Application1",
            "channel-id":1,
            "client-id":"123"
         },
         "relationships":{
            "application-features":{
               "data":[
                  {
                     "type":"application-features",
                     "id":"3"
                  }
               ]
            }
         }
      },
      {
         "type":"channel-applications",
         "id":"4",
         "attributes":{
            "name":"Application2",
            "channel-id":2,
            "client-id":"456"
         },
         "relationships":{
            "application-features":{
               "data":[
                  {
                     "type":"application-features",
                     "id":"7"
                  }
               ]
            }
         }
      },
      {
         "type":"channel-applications",
         "id":"5",
         "attributes":{
            "name":"Application3",
            "channel-id":3,
            "client-id":"001"
         },
         "relationships":{
            "application-features":{
               "data":[
                  {
                     "type":"application-features",
                     "id":"9"
                  },
                  {
                     "type":"application-features",
                     "id":"10"
                  },
                  {
                     "type":"application-features",
                     "id":"11"
                  },
                  {
                     "type":"application-features",
                     "id":"12"
                  }
               ]
            }
         }
      }
   ],
   "included":[
      {
         "type":"application-features",
         "id":"3",
         "attributes":{
            "channel-application-id":2,
            "feature-id":3,
            "scope":"teste1, teste2, teste3",
            "connect-type":"script"
         }
      },
      {
         "type":"application-features",
         "id":"7",
         "attributes":{
            "channel-application-id":4,
            "feature-id":3,
            "scope":"",
            "connect-type":"redirect"
         }
      },
      {
         "type":"application-features",
         "id":"9",
         "attributes":{
            "channel-application-id":5,
            "feature-id":1,
            "scope":"teste4, teste5",
            "connect-type":"redirect"
         }
      },
      {
         "type":"application-features",
         "id":"10",
         "attributes":{
            "channel-application-id":5,
            "feature-id":2,
            "scope":"",
            "connect-type":"password"
         }
      },
      {
         "type":"application-features",
         "id":"11",
         "attributes":{
            "channel-application-id":5,
            "feature-id":3,
            "scope":"",
            "connect-type":"password"
         }
      },
      {
         "type":"application-features",
         "id":"12",
         "attributes":{
            "channel-application-id":5,
            "feature-id":4,
            "scope":"",
            "connect-type":"password"
         }
      }
   ]
}

So, someone know about anything I'm doing wrong, please?




Aucun commentaire:

Enregistrer un commentaire