JSON 对 Ember 数据多态关系的期望
JSON Expectations for Ember Data Polymorphic Relationships
DEBUG: Ember : 1.8.1
ember.js:15373DEBUG: Ember Data : 1.0.0-beta.18
ember.js:15373DEBUG: Handlebars : 1.3.0
ember.js:15373DEBUG: jQuery : 1.11.3
所以我有一个 "Request" 模型,它可以依赖于一个帐户或其他请求,当它完成时代表和帐户。
我有点困惑如何让 JSON 适合 Ember 来选择它。
在服务器端,ActiveModelSerialzers 似乎想要像这样序列化多态关系:
{
"id" : 1,
"account_id" : { "id": 1, "type": "account"}
}
我有以下 ED 型号:
// abstract-account.js
export default DS.Model.extend();
//account.js
export default AbstractAccount.extend();
//request.js
export default DS.Model.extend({
account: DS.belongsTo('abstractAccount', { polymorphic: true} )
});
当我尝试使用默认 json 加载记录时,我得到
Error: Assertion Failed: You must include an `id` for account in an object passed to `push`
在我的阅读中,我得到的印象是 belongsTo 多态关联应该像这样发送:
{
"id" : 1,
"account_id" : 1,
"account_type" : "Account"
}
如果我将服务器更改为那样序列化,我反而会得到
Uncaught Error: Assertion Failed: You may not pass `null` as id to the store's find method
我不太确定我遗漏了什么,或者当前推荐的做法是什么。
我正在使用与第一个约定类似的设置。请记住 Ember 期望 JSON 对象在 class 名称下进行命名空间,因此对于 GET 到 /requests 你需要 return 类似
{
'requests': [
{
'id': 1,
'account': { id: 10, type: 'account' }
},
{
'id': 2,
'account': { id: 20, type: 'some_other_abstract_account' }
}
]
}
这是在application/adapter.js中声明的RESTAdapter,我没试过ActiveModelAdapter。我看过建议 another convention 的文档,但它对我有用 ember 数据 1.0.0-beta.16
DEBUG: Ember : 1.8.1
ember.js:15373DEBUG: Ember Data : 1.0.0-beta.18
ember.js:15373DEBUG: Handlebars : 1.3.0
ember.js:15373DEBUG: jQuery : 1.11.3
所以我有一个 "Request" 模型,它可以依赖于一个帐户或其他请求,当它完成时代表和帐户。
我有点困惑如何让 JSON 适合 Ember 来选择它。
在服务器端,ActiveModelSerialzers 似乎想要像这样序列化多态关系:
{
"id" : 1,
"account_id" : { "id": 1, "type": "account"}
}
我有以下 ED 型号:
// abstract-account.js
export default DS.Model.extend();
//account.js
export default AbstractAccount.extend();
//request.js
export default DS.Model.extend({
account: DS.belongsTo('abstractAccount', { polymorphic: true} )
});
当我尝试使用默认 json 加载记录时,我得到
Error: Assertion Failed: You must include an `id` for account in an object passed to `push`
在我的阅读中,我得到的印象是 belongsTo 多态关联应该像这样发送:
{
"id" : 1,
"account_id" : 1,
"account_type" : "Account"
}
如果我将服务器更改为那样序列化,我反而会得到
Uncaught Error: Assertion Failed: You may not pass `null` as id to the store's find method
我不太确定我遗漏了什么,或者当前推荐的做法是什么。
我正在使用与第一个约定类似的设置。请记住 Ember 期望 JSON 对象在 class 名称下进行命名空间,因此对于 GET 到 /requests 你需要 return 类似
{
'requests': [
{
'id': 1,
'account': { id: 10, type: 'account' }
},
{
'id': 2,
'account': { id: 20, type: 'some_other_abstract_account' }
}
]
}
这是在application/adapter.js中声明的RESTAdapter,我没试过ActiveModelAdapter。我看过建议 another convention 的文档,但它对我有用 ember 数据 1.0.0-beta.16