我尝试设置一个hasMany关系,并希望在单个请求中加载包含所有相关模型的主实体。但这似乎不适用于继承的"hasMany“关系。我有一个定义所有关系和字段的BaseModel和一个定义要加载的代理的“普通”模型。
以下是我的(相关)模型:
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'
}
]
}
]
}
"account“关系在”普通“用户模型上工作,我可以通过自动生成的方法user.getAccount()访问它。
现在,我尝试使用自动生成的方法访问用户的电子邮件:
// user is of 'MyApp.model.User'
user.emails(); // store
user.emails().first(); // null
user.emails().count(); // 0
似乎“电子邮件”-relation模型并没有加载到我的用户模型中。我访问它们的方式正确吗?我可以通过user.data.emails访问它们。但这是一个普通对象的数组,而不是UserEmail对象的数组。
有人能给我一些建议吗?无键关联是否支持嵌套加载?
向您致以亲切的问候
编辑:整理我所写的内容。
发布于 2017-10-06 01:24:42
应该能行得通。这是一个有效的fiddle。检查控制台中是否有嵌套的加载数据。
https://stackoverflow.com/questions/46583000
复制相似问题