javascript - Extjs模型数组的字符串映射

标签 javascript extjs

这似乎是一个非常基本的问题,但恰好占用了我很多时间。

如何将以下内容映射到 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;
    }                       
} 

我也尝试过其他组合,但似乎没有效果。

最佳答案

以下内容是为了适合下面示例中的数据而制作的。

这是一个Sencha Fiddle我提供的答案。它符合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 Forums: How do I parse a XML node to an array of strings? Also handing XML attributes? 已接受的解决方案.

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>

商店

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;
            }
        }] 
    }
});

关于javascript - Extjs模型数组的字符串映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858953/

相关文章:

php - 如何在blueimp文件 uploader 中传递值?

Extjs 4.1 日历 - 工具提示第一次不显示

javascript - ExtJS:如何自定义组合框选择器的键导航?

javascript - 当新记录添加到他的商店时网格不更新

javascript - Object.assign 不是函数

javascript - 在 PHP 中使用 Ajax 向数据库中插入数据

javascript - 如何在 JavaScript 中等待递归方法调用

javascript - 我需要 "Autolisting"函数来编辑下拉列表

javascript - 尝试使用鼠标滚轮时垂直滚动条不起作用#FF,Chrome

javascript - 将组件渲染到摘要行