javascript-嵌套XML数据和ExtJS模型关联
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-嵌套XML数据和ExtJS模型关联,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3460字,纯文字阅读大概需要5分钟。
内容图文
![javascript-嵌套XML数据和ExtJS模型关联](/upload/InfoBanner/zyjiaocheng/675/5462b9ceb62a43cea0a2da90b9ef73b6.jpg)
我正在尝试将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'
}]
});
一对一:
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:无法调用未定义的方法’indexOf’”,然后为关联数据输出获得一个对象,该对象充满了具有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
内容总结
以上是互联网集市为您收集整理的javascript-嵌套XML数据和ExtJS模型关联全部内容,希望文章能够帮你解决javascript-嵌套XML数据和ExtJS模型关联所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。