我试图摆脱 Ember-Data 中的一些异步关系并改为侧载它们,但遇到了一些问题。
我的 API 发回相关数据,但记录是返回的每个对象的属性,而不是根上的单独数组/对象。我extractArray
用来处理数据,所以 ED 喜欢它,但不能完全正确。以下是从 API 返回的数据示例:
{
"record": [
{
"id": 2,
"name": "3M Aerospace",
"currency": 6,
"paymentTerms": 3,
"Currencies_by_currency": {
"id": 6,
"currency": "USD",
"description": "US Dollar",
"sortOrder": 1
},
"PaymentTerms_by_paymentTerms": {
"id": 3,
"term": "NET10",
"description": "Due with 10 days of invoice date"
"sortOrder": 3
}
},
{
"id": 3,
"name": "BAE Aerospace",
"currency": 6,
"paymentTerms": 3,
"Currencies_by_currency": {
"id": 6,
"currency": "USD",
"description": "US Dollar"
"sortOrder": 1
},
"PaymentTerms_by_paymentTerms": {
"id": 3,
"term": "NET10",
"description": "Due with 10 days of invoice date"
"sortOrder": 3
}
}
]
}
我知道我需要从对象根中获取Countries_by_mailingAddressCountry
、Currencies_by_currency
和。PaymentTerms_by_paymentTerms
他们究竟应该去哪里?我认为应该是:
{
"record": [
{
"id": 2,
// other data...
"currency": 6,
"paymentTerms": 3,
// more data...
}, {
"id": 3,
// other data...
"currency": 6,
"paymentTerms": 3,
// more data...
}
],
"currencies": [
{
"id": 6,
"currency": "USD",
"description": "US Dollar",
"sortOrder": 1
}
],
"paymentTerms": [
{
"id": 3,
"term": "NET10",
"description": "Due with 10 days of invoice date",
"isActive": true,
"sortOrder": 3
}
]
}
但 ED 抱怨找不到相关数据。模型定义为
App.Vendor = DS.Model.extend(App.Addressable, {
name: DS.attr('string'),
currency: DS.belongsTo('currency'),
paymentTerms: Ds.belongsTo('payment-term')
});
App.Currency = DS.Model.extend({
currency: string,
description: string,
sortOrder: number,
});
App.PaymentTerm = DS.Model.extend({
term: string,
description: string,
sortOrder: number,
});