使用JSONAPI访问hasMany / belongsTo关系数据

时间:2016-10-06 08:54:51

标签: javascript ember.js ember-data json-api

请帮帮我吗?

我有一个模型Channel,一个模型Feature,一个模型ChannelApplication和一个ModelFeature模型。我把它们定义为:

我的模型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 })
});

我的模型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 })
});

我的模型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 })
});

我的模型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 })
});

我正在使用 JSONAPI 。在我的路线中,我这样做:

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

因此,在我的模板中,我需要获取ChannelApplications,并且我需要知道与Channel相关的每个ChannelApplications,并了解每个ApplicationFeature

{{#each channelApplications as |channelApplication|}}
    {{channelApplication.id}}

    // I think in something like this, but this doesn't work of course
    {{#each channelApplication.channels as |channel|}}
       <span>{{channel.id}}</span>
       <span>{{channel.name}}</span>
       {{#each channelApplication.applicationFeatures as |applicationFeature|}}
          <span>{{applicationFeature.id}}</span>
          <span>{{applicationFeature.name}}</span>
        {{/each}}
    {{/each}}

{{/each}}

channelApplication.applicationFeatures channelApplication.channels 不会返回任何内容。

当我打印{{channelApplication.applicationFeatures.length}}时,它返回0.

当我打印{{channelApplication.applicationFeatures}}时,它会打印<DS.PromiseManyArray>

channelApplications: this.store.query('channel-application', { filter: { feature: 'inbox'} })的JSONAPI返回:

{
   "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"
         }
      }
   ]
}

所以,有人知道我做错了什么,拜托?

1 个答案:

答案 0 :(得分:0)

您的回答不正确channel-application。另外还不清楚:channel是否有一个或多个channel: belongsTo('channel'), s?

因为channel表示它有一个{{#each channelApplication.channels as |channel|}}但是你不应该像channel一样遍历它们。

如果您有一个"channel-id":1,,则无法将channel: { data: { type: 'channel', id: '1' } } 作为属性返回,但应该返回关系:

 InputMethodManager inputMethodManager =
        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInputFromWindow(
        linearLayout.getApplicationWindowToken(),
        InputMethodManager.SHOW_FORCED, 0);
相关问题