我尝试设置一种hasmany关系,并希望在单个请求中加载我的主实体。但这似乎与继承的" hasmany"关系不起作用。我有一个基本模型定义所有关系和字段以及一个"正常"模型,该模型定义了代理。
这些是我的(相关)模型:
Ext.define('MyApp.model.BaseUser', {
"extend": "Ext.data.Model",
"uses": [
"MyApp.model.UserEmail",
"MyApp.model.Account"
],
"fields": [
{
"name": "name"
},
{
"name": "accountId",
"reference": {
"type": "MyApp.model.Account",
"role": "account",
"getterName": "getAccount",
"setterName": "setAccount",
"unique": true
}
}
]
"hasMany": [
{
"name": "emails",
"model": "MyApp.model.UserEmail"
}
],
});
Ext.define('MyApp.model.User', {
extend: "MyApp.model.BaseUser",
proxy: {
type: 'rest',
url : '/api/user',
reader: {
type: 'json',
rootProperty: 'data',
}
}
});
Ext.define('MyApp.model.UserEmail', {
extend: 'Ext.data.Model',
"fields": [
{
"name": "id",
"type": "int"
},
{
"name": "email",
"type": "string"
},
],
proxy: {
type: 'rest',
url : '/api/user/email',
reader: {
type: 'json',
rootProperty: 'data',
}
}
});
// MyApp.model.Account looks like MyApp.model.UserEmail
这是我的服务器的响应:
{
data: [
{
name: 'User Foo'
accountId: 50
account: {
id: 50,
balance: 0
},
emails: [
{
id: 70,
email: 'hello@world.de'
}
]
}
]
}
"帐户"关系正在使用"普通"用户模型,我可以通过自动生成的方法user.getAccount()访问它。
现在,我尝试使用自动生成方法访问用户电子邮件:
// user is of 'MyApp.model.User'
user.emails(); // store
user.emails().first(); // null
user.emails().count(); // 0
看来"电子邮件" - 汇总模型未加载到我的用户模型中。我是用正确的方式访问它们吗?我可以通过user.data.emails访问它们。但这是一系列普通对象,而不是用户邮件 - 对象。
谁能给我一些建议?嵌套加载是否由无钥匙关联支撑?
亲切的问候,czed
编辑:清除了我的内容。
它应该起作用。这是一个工作的小提琴。检查嵌套加载数据的控制台。