我的ExtJS(4.2.1)应用程序中有两个模型,并希望在两个模型之间定义“hasOne”关联。但是,我无法使其发挥作用。
我试图尊重http://extjs-tutorials.blogspot.ch/2012/05/extjs-belongsto-association-rules.html中所示的“hasOne”关联规则。
主要模式
Ext.define('Aap.model.TreeNode', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: "string"},
{ name: 'id'},
{ name: 'allgemein_id'}
],
proxy: {
type: 'ajax',
url: 'data/treedata.json',
reader: {
type: 'json',
root: 'nodes',
successProperty: 'success'
}
},
associations: [
{
type: 'hasOne',
model: 'Aap.model.Allgemein',
associationKey: 'metaallgemein',
primaryKey: 'id',
foreignKey: 'allgemein_id',
getterName: 'getAllgemein',
setterName: 'setAllgemein',
autoLoad: true
}
]
});
关联模式
Ext.define('Aap.model.Allgemein', {
extend: 'Ext.data.Model',
requires: 'Aap.model.TreeNode',
fields: [
{name: "name", type: 'string'},
{name: "allgemein_name", type: 'string'},
{name: "ident", type: 'string'},
{name: "georefdat", type: 'string'},
{name: "fachst", type: 'string'},
{name: "zugberech", type: 'string'},
{name: "echkateg", type: 'string'},
{name: "nachfzeitr", type: 'string'},
{name: "datenmenge"},
{name: "imjr"},
{name: "datenzuw"},
{name: "bemerk", type: 'string'},
{name: "treenode_id", type: 'int'},
{name: "id", type: "int"}
],
associations: [
{
type: 'belongsTo',
model: 'Aap.model.TreeNode'
}
],
proxy: {
type: 'ajax',
api: {
read: 'data/allgemeindata.json'
},
reader: {
type: 'json',
root: 'metaallgemein',
successProperty: 'success'
}
}
});
主要模型的数据
{
"success": true,
"nodes": [
{ "name": "st", "id":"1", "allgemein_id": "1", "expanded": false, "children": [
{ "name": "Geodätische Grundlagen", "id":"4", "allgemein_id": "4", "children": [
{ "name": "Beispiel Datensatz", "id":"6", "allgemein_id": "6", "leaf": true }
]},
{ "name": "Bilddaten", "id":"5", "allgemein_id": "5", "loaded": true}
]},
{ "name": "BU", "id":"2", "allgemein_id": "2", "loaded": true },
{ "name": "BE", "id":"3", "allgemein_id": "3", "loaded": true }
]
}
相关模型的数据
{
"success": true,
"metaallgemein": [
{
"name":"st",
"ident": "stident",
"georefdat":"stgeorefdat",
"fachst":"stfach",
"zugberech":"stzugangsbe",
"echkateg":"stechkateg",
"nachfzeitr":"stzeitrnachf",
"datenmenge":"stdatenmende",
"imjr":"stimjr",
"datenzuw":"stdatenzuw",
"bemerk":"stbemerk",
"id": "1",
"treenode_id": "ext-record-1"
},
{
"name":"BU",
"ident": "buident",
"georefdat":"bugeorefdat",
"fachst":"bufach",
"zugberech":"buzugangsbe",
"echkateg":"buechkateg",
"nachfzeitr":"buzeitrnachf",
"datenmenge":"budatenmende",
"imjr":"buimjr",
"datenzuw":"budatenzuw",
"bemerk":"bubemerk",
"id": "2",
"treenode_id": "ext-record-2"
}, ...
...
...
当我加载页面并尝试使用我的getter获取相关数据时,会发生以下情况(参见图片)。
我尝试了一个更简单的例子来跟踪问题。但我甚至没有实现使协会在最基本的例子中工作。似乎我在某处犯了一个基本的错误:
Ext.define('Aap.model.Person', {
extend: 'Ext.data.Model',
requires: 'Aap.model.Address',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'address_id', type: 'int'}
],
proxy: {
type: 'ajax',
api: {
read: 'data/persons.json'
},
reader: {
type: 'json',
root: 'children',
successProperty: 'success'
}
},
associations: [
{
type: 'hasOne',
model: 'Aap.model.Address'
}
]
});
Ext.define('Aap.model.Address', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int'},
{ name: 'city', type: 'string'}
],
proxy: {
type: 'ajax',
api: {
read: 'data/addresses.json'
},
reader: {
type: 'json',
root: 'children',
successProperty: 'success'
}
}
});
Ext.define('Aap.store.Persons', {
extend: 'Ext.data.Store',
model: 'Aap.model.Person',
autoLoad: 'true',
storeId: 'persons'
});
Ext.define('Aap.store.Addresses', {
extend: 'Ext.data.Store',
model: 'Aap.model.Address',
autoLoad: 'true',
storeId: 'addresses'
});
{
"success": true,
"children": [
{
"id": 1,
"name": "Bernt",
"address_id": 1
},
{
"id": 2,
"name": "Ludowid",
"address_id": 2
}
]
}
{
"success": true,
"children": [
{
"id": 1,
"city": "Berlin"
},
{
"id": 2,
"city": "London"
}
]
}
我不知道问题出在哪里。
博客条目如: ExtJS 4: Models with Associations and Stores 让我更好地了解协会的运作方式。但他们没有帮我解决问题。
感谢您的帮助!
答案 0 :(得分:0)
生成的getter应该异步使用,不能依赖实际的返回值。
// Don't do that
var unreliableResult = m.getAllgemein();
// Do that instead
m.getAllgemein(function(allgemeinRecord) {
// continue from here...
});
有关详情,请参阅doc中名为“Generated getter and setter”的部分。
但请注意,回调实现似乎有点buggy。
现在,关于问题的“错误”数据方面,您的关联配置对我来说似乎是正确的。我快速测试了你的代码,并且正确的请求(也就是关联记录的正确id)被解雇了......所以也许你应该详细说明为什么你认为数据是“错误的”,有人可能会发现错误然后