如何创建具有多个属于相同模型类型的 ExtJS 4 模型?
我对 ExtJS 还很陌生,并且有点挣扎。
我有一个“人”模型,它与同一模型(母亲和父亲)有几种关系。
但我不清楚如何访问这些关系。这是我所得到的:
Ext.define('IwiDb.model.Person', {
extend: 'Ext.data.Model',
fields: [
'ID',
'Title',
'FirstName',
'MiddleNames',
'Surname',
//.. snip
'MotherID',
'FatherID',
],
associations: [{
type: 'belongsTo',
model: 'IwiDb.model.Person',
primaryKey: 'ID',
foreignKey: 'MotherID',
autoLoad: true,
name: 'Mother',
getterName: 'getMother',
},{
type: 'belongsTo',
model: 'IwiDb.model.Person',
primaryKey: 'ID',
foreignKey: 'FatherID',
autoLoad: true,
name: 'Father',
getterName: 'getFather',
}],
idProperty: 'ID',
proxy: {
type: 'rest',
url: 'api/v1.extjs/Person',
format: 'json',
reader: {
type: 'json',
root: 'items',
totalProperty: 'totalSize',
},
writer: {
type: 'json',
},
simpleSortMode: true,
}
});
编辑:我认为我已经接近了,我已经将代码更新为我现在所拥有的。至少现在我可以执行 person.getMother() 并返回一个函数。但我还是拿不到数据。有人知道怎么做吗?
I'm fairly new to ExtJS and am struggling with it a little.
I have a "Person" model which has a couple of relationships to the same model (Mother and Father).
I'm not clear on how to access these relationships though. Here's what I've got:
Ext.define('IwiDb.model.Person', {
extend: 'Ext.data.Model',
fields: [
'ID',
'Title',
'FirstName',
'MiddleNames',
'Surname',
//.. snip
'MotherID',
'FatherID',
],
associations: [{
type: 'belongsTo',
model: 'IwiDb.model.Person',
primaryKey: 'ID',
foreignKey: 'MotherID',
autoLoad: true,
name: 'Mother',
getterName: 'getMother',
},{
type: 'belongsTo',
model: 'IwiDb.model.Person',
primaryKey: 'ID',
foreignKey: 'FatherID',
autoLoad: true,
name: 'Father',
getterName: 'getFather',
}],
idProperty: 'ID',
proxy: {
type: 'rest',
url: 'api/v1.extjs/Person',
format: 'json',
reader: {
type: 'json',
root: 'items',
totalProperty: 'totalSize',
},
writer: {
type: 'json',
},
simpleSortMode: true,
}
});
EDIT: I think I'm getting close, I've updated the code to what I've got now. At least now I can do person.getMother() and get a function back. But I still can't get the data. Anyone know how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我最后想通了。上面的(编辑过的)代码是正确的定义,那么如果我想加载母亲,我会这样做:
Well, I figured it in the end. The (edited) code above was the correct definition, then if I wanted to eg load the mother, I do something like this: