Access与JSONAPI的关系数据有很多/属于
Access hasMany/belongsTo relationship data with JSONAPI
你能帮我吗?
我有一个模型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
,对于每一个,我需要知道与这个ChannelApplications
相关的Channel
,并且知道每个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.cahannels不返回任何内容。
当我打印{{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"
}
}
]
}
那么,有人知道我做错了什么吗?
您的响应不正确JSONAPI
。同样也不清楚:一个channel-application
有一个或多个channel
吗?
因为channel: belongsTo('channel'),
意味着它有一个channel
,但你不应该像{{#each channelApplication.channels as |channel|}}
那样循环它们。
如果您有一个channel
,则不能将"channel-id":1,
作为属性返回,但应该返回一个关系:
channel: {
data: {
type: 'channel',
id: '1'
}
}
- 用程序搜索JQuery数据表中的文本
- 要求输入在数据列表中
- 正在将数据主题添加到所有项目
- 函数参数中的数据与指定变量之间的任何性能差异
- 在VanillaJS中模拟模型双向数据绑定
- CSS-如何定位内容数据标题
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 序列化数据属性中对象的最可靠方法
- 如何将JSON数据导入我的ejs模板
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 画布数据到图像
- 使用jquery将mysql数据获取到新的表行中
- 使用html中的外部javascript进行数据验证
- 有时数据是't显示在浏览器中
- Ajax数据不属于成功范畴
- Access与JSONAPI的关系数据有很多/属于
- 使用 Ember.js 加载属于父模型的数据
- 我如何知道哪个请求属于node.js中的哪些部分数据