嵌套的XML数据和ExtJS模型关联
Nested XML data and ExtJS model associations
我正在尝试将XML数据映射到ExtJS中的一系列模型。我能够从顶层模型中提取数据,但是,与它的关联无法检索到必要的数据。
以下是我的所有型号:
Ext.define('app.model.ProductType', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [{
name: 'id',
mapping: 'id',
type: 'int'
}, {
name: 'name',
mapping: 'name',
type: 'string'
}, {
name: 'sometag',
mapping: 'sometag',
type: 'string'
}],
associations: [{
type: 'hasMany',
model: 'app.model.Address',
name: 'addresses',
autoLoad: true,
reader: {
type: 'xml',
record: 'address',
root: 'addressList'
}
}, {
type: 'hasMany',
model: 'app.model.PhoneContact',
name: 'contacts',
autoLoad: true,
reader: {
type: 'xml',
record: 'phoneContact',
root: 'phoneContactList'
}
}, {
type: 'hasOne',
model: 'app.model.OneToOne',
name: 'oneToOne',
autoLoad: true,
reader: {
type: 'xml',
record: 'oneToOne'
}
}],
proxy: {
type: 'ajax',
url: 'data/example.xml',
reader: {
type: 'xml',
record: 'ProductType',
root: 'ProductResultSet'
}
}
});
地址:
Ext.define('app.model.Address', {
extend: 'Ext.data.Model',
fields: [{
name: 'city',
mapping: 'city',
type: 'string'
}, {
name: 'street',
mapping: 'street',
type: 'string'
}],
associations: [{
type: 'belongsTo',
model: 'app.model.ProductType'
}]
});
电话联系人:
Ext.define('app.model.PhoneContact', {
extend: 'Ext.data.Model',
fields: [{
name: 'primary',
mapping: 'primary',
type: 'string'
}, {
name: 'cell',
mapping: 'cell',
type: 'string'
}],
associations: [{
type: 'belongsTo',
model: 'app.model.ProductType'
}]
});
OneToOne:
Ext.define('app.model.OneToOne', {
extend: 'Ext.data.Model',
fields: [{
name: 'first',
mapping: 'firstfield',
type: 'string'
}, {
name: 'second',
mapping: 'secondfield',
type: 'string'
}],
associations: [{
type: 'belongsTo',
model: 'app.model.ProductType'
}]
});
这是我的app.js
Ext.Loader.setConfig({
enabled: true
});
Ext.application({
name: 'app',
autoCreateViewport: false,
requires: ['app.model.ProductType', 'app.model.Address', 'app.model.PhoneContact', 'app.model.OneToOne', 'app.store.ProductTypeStore'],
launch: function () {
app.model.ProductType.load(55, {
scope: this,
callback: function (record, operation) {
console.log('Record Addresses:');
console.log(record.addresses());
}
});
var s = app.store.ProductTypeStore.create();
s.on('load', this.wiggidy, this);
s.load();
},
wiggidy: function (store, records, success, eOpts) {
var rec = store.getAt(0);
console.log('Associated Data:');
console.log(rec.getAssociatedData());
}
});
最后,example.xml
<ProductResultSet>
<ProductType>
<id>55</id>
<name>Product Name</name>
<sometag>asdf</sometag>
<addressList>
<address>
<street>12345 a</street>
<city>myCity</city>
</address>
<address>
<street>234567 b</street>
<city>Denver</city>
</address>
</addressList>
<phoneContactList>
<phoneContact>
<primary>234</primary>
<cell>4667</cell>
</phoneContact>
<phoneContact>
<primary>5467</primary>
<cell>87904</cell>
</phoneContact>
</phoneContactList>
<oneToOne>
<firstField>value</firstField>
<secondField>value</secondField>
</oneToOne>
</ProductType>
</ProductResultSet>
控制台为RecordAddresses输出输出"Uncaught TypeError:Cannot call method'indexOf'of undefined",然后我为Associated Data输出得到一个填充了0对象数组的对象。
我完全被这件事难住了。我到处找了找,走了这么远。为什么记录没有加载该xml文件中的嵌套数据?
任何帮助都将不胜感激。
为什么在hasMany关系中设置autoLoad=true?
您试图使用嵌套数据,然后告诉它按需加载数据,并想知道为什么没有嵌套数据:)
您还缺少嵌套加载所需的associationKey。
以下是您的代码的一个工作示例:
http://jsfiddle.net/el_chief/668Fg/5/
在未来遵循我的"规则",一切都会好起来的:
http://extjs-tutorials.blogspot.ca/2012/05/extjs-hasmany-relationships-rules.html
相关文章:
- 如何将本地json数据加载到Extjs数据模型中
- ExtJS 4:编写带有模型关联的嵌套XML
- 在extJS 4.2中,有没有一种方法可以将模型值动态绑定到表单
- 将 ExtJS 代理 AJAX 响应映射到模型
- 字符串映射的Extjs模型数组
- 嵌套的XML数据和ExtJS模型关联
- 如何忽略ExtJS数据模型中的空字段
- 我可以使用sencha extjs商店和模型进行sencha触摸吗
- 通过 id-Extjs 将数据加载到模型中
- 在 ExtJS 4 应用程序之间共享模型
- ExtJS 5:父模型转换对子关联的依赖关系
- 在 extjs 3.3.1 列模型中打印未隐藏的列
- 如何为此 xml 格式创建 EXTJS 模型
- 尝试在列模型 ExtJS 3.3.1 中记录隐藏和非隐藏列
- ExtJS网格选择模型
- Extjs使用复选框模型在网格上拖放单个项目
- 如何从模型存储EXTJs中读取额外属性
- 如何实现期望接收对象列表的Extjs模型
- 如何绑定 ExtJS 模型和挖空模板
- extjs模型覆盖