字符串映射的Extjs模型数组
Extjs model array of string mapping
这似乎是一个非常基本的问题,但碰巧浪费了我很多时间。
如何将以下内容映射到Ext.data.Model
?
<Principal>STEMMED CORP.</Principal>
<BusinessDefinitions>
<value>NY CLIENTS CORE</value>
<value>US LISTED DERIVS- STOCK,ADR,ETF</value>
<value>US CLIENT SITE TRADING</value>
<value>SYNDICATES - ADRS AND CBS</value>
<value>GWM CAPITAL MARKETS</value>
</BusinessDefinitions>
<countryOfResidence>USA</countryOfResidence>
问题是,我不知道如何根据每个值获得BusinessDefinitions
的字符串数组。
以下字段是我添加到Model.fields
中的内容:
// Business Definition(s)
{
name: 'BusinessDefinitions',
type: 'auto',
mapping: 'value',
convert: function(n, record) {
console.log('BusinessDefinition: ' + n);
return n;
}
}
我也尝试过其他组合,但似乎都不起作用。
以下内容是为了适应下面示例中的数据而制作的。
以下是我提供的答案。符合4.2.1.883。我还没有尝试这个版本5.1.0。
数据
<BusinessArray>
<BusinessItem>
<Principal>STEMMED CORP.</Principal>
<BusinessDefinitions>
<value>NY CLIENTS CORE</value>
<value>US LISTED DERIVS- STOCK,ADR,ETF</value>
<value>US CLIENT SITE TRADING</value>
<value>SYNDICATES - ADRS AND CBS</value>
<value>GWM CAPITAL MARKETS</value>
</BusinessDefinitions>
<countryOfResidence>USA</countryOfResidence>
</BusinessItem>
</BusinessArray>
应用程序
Ext.define('App.model.Business', {
requires: [ 'Ext.data.reader.Xml' ],
extend: 'Ext.data.Model',
fields: [{
name: 'principal',
mapping: 'Principal',
type: 'string'
}, {
name: 'country',
mapping: 'countryOfResidence',
type: 'string'
}, {
name: 'businessDefs',
type : 'auto',
convert: function(value, record) {
var nodes = record.raw.querySelectorAll('BusinessDefinitions value');
var items = [];
for (var i = 0; i < nodes.length; i++) {
items.push(nodes[i].textContent);
}
return items;
}
}]
});
Ext.application({
name : 'Fiddle',
launch : function() {
var store = Ext.create('Ext.data.Store', {
model: 'App.model.Business',
proxy: {
type: 'ajax',
url: 'business.xml',
reader: {
type: 'xml',
record: 'BusinessItem'
}
},
autoLoad : true
});
Ext.create('Ext.panel.Panel', {
title : 'XML Model Example',
layout : 'hbox',
items : [{
xtype: 'combo',
fieldLabel: 'Business',
emptyText: 'select',
editable: false,
queryMode: 'local',
store: store,
displayField: 'principal',
valueField: 'businessDefs',
listeners : {
select: function (combo, record, index) {
Ext.Msg.alert('Business Definitions', combo.getValue().join('<br />'));
}
}
}],
renderTo: Ext.getBody()
});
}
});
示例
下面的例子来自Sencha论坛的公认解决方案:如何将XML节点解析为字符串数组?还要处理XML属性?。
XML数据
<jobs>
<job>
<id>1</id>
<name audioSrc="audio/jobs/names/electrician.mp3">Electrician</name>
<attributes>
<attributeID>sitting</attributeID>
<attributeID>individual</attributeID>
<attributeID>lightEquip</attributeID>
<attributeID>doer</attributeID>
<attributeID>physical</attributeID>
<attributeID>repair</attributeID>
</attributes>
</job>
</jobs>
Store
Ext.create('Ext.data.Store', {
model: 'App.model.JobData',
proxy: {
type: 'ajax',
url: dataURL,
reader: {
type: 'xml',
record: 'job'
}
}
});
型号
Ext.define('App.model.JobData', {
requires: [ 'Ext.data.reader.Xml' ],
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'id',
mapping: 'id',
type: 'int'
}, {
name: 'title',
mapping: 'name',
type: 'string'
}, {
name: 'attributeList',
mapping : 'attributes',
convert: function(value, record) {
var nodes = record.raw.querySelectorAll('attributes attributeID');
var arrayItem = [];
var l = nodes.length;
for (var i = 0; i < l; i++) {
var node = nodes[i];
arrayItem.push(nodes[i].textContent);
console.log(nodes[i].textContent);
}
return arrayItem;
}
}]
}
});
以下是对我有效的方法:无需添加requires: [ 'Ext.data.reader.Xml' ],
接下来是最后一个字段
},{
//Business Definition/s
name: 'BusinessDefinitions',
type: 'auto',
mapping: function(data){
return data.children[2].children[10];
},
convert: function(value, record) {
var items = [];
var nodes = record.data.BusinessDefinitions.querySelectorAll('BusinessDefinitions value');
for (var i = 0; i < nodes.length; i++) {
items.push(nodes[i].textContent);
}
return items;
}
},
相关文章:
- 使用knockout.js将数组绑定到视图模型
- 将js对象更改为使用嵌套的可观察数组敲除js视图模型
- 如何在ember数据模型中表示数组
- 在Backbone中对模型(数组)进行排序
- 主干输出用于集合模型的空数组
- 字符串映射的Extjs模型数组
- Angular2没有在模型数组中迭代
- Ember 数据模型数组并不总是有数据
- AngularJS 无法自定义验证模型数组值
- 如何在不破坏角度模型(数组)的情况下对其进行过滤
- 主干.js - 模型数组上的更改事件不会在元素更改时触发
- Backbone-JSON的模型数组
- 主干模型数组属性更改和更改事件侦听器不总是启动
- 如何创建对象的Mongoose模型数组
- 无法清除主干模型数组属性
- 如何将控制器's content属性设置为模型数组,而不需要相应的路由
- VueJS复选框模型数组的整数
- 主干:如何在添加模型数组时仅触发一次添加事件
- 筛选骨干集合将返回一个模型数组
- 如何将模型数组传递到jquery中