ExtJS:如何将商店字段映射到JSON属性?

时间:2016-09-29 02:07:02

标签: javascript json extjs data-binding

我有以下JSON数据:

{
  "disclaimer": "Exchange rates provided for informational purposes only and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, no guarantees are made of accuracy, validity, availability, or fitness for any purpose. All usage subject to acceptance of Terms: https://openexchangerates.org/terms/",
  "license": "Data sourced from various providers; resale prohibited; no warranties given of any kind. All usage subject to License Agreement: https://openexchangerates.org/license/",
  "timestamp": 1475110853,
  "base": "USD",
  "rates": {
    "AED": 3.672983,
    "AFN": 66.5538,
    "ALL": 122.0421,
    "AMD": 473.5925,
    "ANG": 1.7763,
    "AOA": 165.571834,
    "ARS": 15.3169,
    "AUD": 1.299338,
    "AWG": 1.792667,
    "YER": 250.130999,
    "ZAR": 13.61321,
    "ZMK": 5252.024745,
    "ZMW": 9.831204,
    "ZWL": 322.387247
  }
}

我已将我的模型定义如下:

Ext.define('CurrencyConvert.model.CurrencyCode', {
    extend : 'Ext.data.Model',
    fields : [
        {
            name : 'code',
            value : 'string'
        },
        {
            name : 'rate',
            value : 'float'
        }
    ]
});

这样我就能拥有货币代码(即" USD")和汇率。但问题是货币代码本身是实际利率的属性名称;那么我如何创建我的商店以获得我的模型中的代码和速率?

例: 对于"AED": 3.672983,我希望code值保持" AED"和rate字段以容纳3.672983。

2 个答案:

答案 0 :(得分:0)

你可以这样做:


    Ext.define('CurrencyConvert.model.CurrencyCode', {
        extend : 'Ext.data.Model',
        fields : [
            {
                name : 'code',
                value : 'string',
                convert:function(v,rec){
                   === Add Your Logic Here ===
                }
            },
            {
                name : 'rate',
                value : 'float'
                convert:function(v,rec){
                   === Add Your Logic Here ===
                }
            }
        ]
    });

答案 1 :(得分:0)

Ext.define('CurrencyConvert.model.CurrencyCode', {
    extend : 'Ext.data.Model',
    fields : [
        {
            name : 'code',
            value : 'string'
        },
        {
            name : 'rate',
            value : 'float', 
            convert : function(value, record) {
                return jsonData.rates[record.get('code')]
            }
        }
    ]
});