我在建立这种关系时遇到了一些困难。有人可以帮忙吗?
租户有一个应用程序
App有品牌
大问题是2级侧面加载。我当前的API返回如下响应:
{
"tenants": [
{
"app_id": "app_001",
"id": 1,
"name": "Illya_no_delete",
}
],
"app": [
{
"brand_id": "brand_001",
"id": "app_001",
"name": "updated test client 2 name",
}
],
"brands": [
{
"id": "brand_001",
"app_id": "app_001"
}
]
}
我已将模型设置为:
// models/tenant.js
export default Model.extend({
app: belongsTo('app'),
});
// mirage/models/app.js
export default Model.extend({
brand: belongsTo('brand'),
tenant: belongsTo('tenant')
});
// mirage/models/brand.js
export default Model.extend({
app: belongsTo('app')
});
我已将序列化程序设置为:
// mirage/serializers/app.js
export default ApplicationSerializer.extend({
include: ['brand']
});
// mirage/serializers/tenant.js
export default ApplicationSerializer.extend({
include: ['app']
});
我已将方案设置为:
// mirage/scenarios/default.js
let brand = server.create('brand');
let app = server.create('app', {brand: brand});
server.createList('tenant', 10, {app: app});
不幸的是,我生成的响应不会返回侧面加载的品牌对象。我看到了所有其他物品:
{
tenant: {
id: 1,
app_id: 2
},
apps: [
{
id: 2,
brand_id: null,
tenant_id: 1
}
]
}
有关如何让“品牌”出现我需要做些什么的建议?
答案 0 :(得分:0)
对于任何有兴趣的人,根据Sam Selikoff的评论,这里有一个对我有用的解决方案。公开反馈:
<强>型号:强>
// models/tenant.js
export default Model.extend({
app: belongsTo('app'),
brand: belongsTo('brand'),
});
// mirage/models/app.js
export default Model.extend({
tenant: belongsTo('tenant')
});
// mirage/models/brand.js
export default Model.extend({
});
<强>串行器:强>
// mirage/serializers/tenant.js
export default ApplicationSerializer.extend({
include: ['app', 'brand'],
serialize() {
let json = ApplicationSerializer.prototype.serialize.apply(this, arguments);
if (json.brands.length > 0) {
let brandId = json.brands[0].id;
json.applications[0].brand_id = brandId;
}
return json;
}
});
默认情景:
// mirage/scenarios/default.js
let brand = server.create('brand');
let app = server.create('app');
server.createList('tenant', 10, {app: app, brand: brand});
<强>响应:强>
{
tenant: {
id: 1,
app_id: 2
},
apps: [
{
id: 2,
brand_id: 3,
tenant_id: 1
}
],
brand: [
{
id: 3
}
]
}