我正在尝试将 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: Cannot call method 'indexOf' of undefined”,然后我得到一个对象,该对象填充了关联数据输出的 0 个对象的数组。
我完全被这个难住了。我到处搜索,它把我带到了这么远。为什么记录没有加载该 xml 文件中的嵌套数据?
任何帮助将不胜感激。