2016-10-06 24 views
0

请问您能帮我吗?使用JSONAPI访问hasMany/belongsTo关系数据

我有一个模型Channel,模型Feature,模型ChannelApplication和模型ApplicationFeature。我将它们定义是这样的:

我的模型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.applicationFeatureschannelApplication.channels没有返回。

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

当我打印{{channelApplication.applicationFeatures}}它打印channelApplications: this.store.query('channel-application', { filter: { feature: 'inbox'} })一个<DS.PromiseManyArray>

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

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

回答

0

您的回复不正确JSONAPI。也不清楚:channel-application有一个或多个channel

因为channel: belongsTo('channel'),意味着它有一个channel,但你不应该像{{#each channelApplication.channels as |channel|}}一样循环它们。

如果你有一个channel你不能返回"channel-id":1,作为一个属性,但应该返回一个关系:

channel: { 
    data: { 
    type: 'channel', 
    id: '1' 
    } 
}