我正试图摆脱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,
});
答案 0 :(得分:1)
最后我不得不将我的json重新格式化为以下格式
{
"vendors": [
// vendor objects
],
"currencies": [
// currency objects
],
"paymentTerms": [
// paymentTerms objects
]
}
一旦我正确格式化并重构了我的适配器,一切都很好吃!