ExtJS:将商店连接到组合框?

时间:2016-10-01 20:58:00

标签: javascript extjs

在下面的代码中,我手动将记录添加到商店(称为dataStore)。但是当我尝试将商店加载到组合框(标记为cmb)时,我收到以下错误:

  

http://localhost:1841/CurrencyConvert.model.CurrencyCode?_dc=147hj_126&query=&page=1&start=0&limit=25   404(未找到)

看起来好像商店正在尝试调用代理来加载数据(即使它已经包含所有数据,由我手动加载)。以下是商店:

Ext.define('CurrencyConvert.store.CurrencyCode', {
    extend : 'Ext.data.Store',
    model : 'CurrencyConvert.model.CurrencyCode',
    storeId : 'currencyCode',
    addRate : function(currencyCode, currencyRate) {
        this.add({
            code : currencyCode,
            rate : currencyRate
        });
    }
});

Ajax请求使用商店:

Ext.Ajax.request({
                    url : 'data.json',
                    method : 'GET',
                    dataType: 'currency.json',
                    item : cmb,
                    success: function(response)
                    {
                        console.log('Success');
                        var result = Ext.JSON.decode(response.responseText);
                        var currencyCodes = Object.keys(result.quotes);
                        this.dataStore = Ext.create('CurrencyConvert.store.CurrencyCode', {});
                        for( var x = 0; x < currencyCodes.length; x++)
                        {
                            this.dataStore.addRate(currencyCodes[x], result.quotes[currencyCodes[x]]);
                            console.log(this.dataStore.getAt(x).get('code') + " : " + this.dataStore.getAt(x).get('rate'));
                        }
                        cmb.emptyText = "-Chose Currency-";
                        cmb.store = this.dataStore;
                        console.log("Stuff: " + this.dataStore.getAt(2).get('code') + " : " + this.dataStore.getAt(2).get('rate'));         
                        cmb.displayField = 'code';
                        cmb.valueField = 'code';        


                    },
                    failure: function(response) {
                        console.log('Failed!');
                        console.log(response);
                    },
                });

以下是我提取的示例数据:

{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1475354167,
  "source":"USD",
  "quotes":{
    "USDAED":3.672904,
    "USDAFN":65.550003,
    "USDALL":122.239998,
    "USDAMD":473.959991,
    "USDANG":1.770403,
    "USDAOA":165.067001,
    "USDARS":15.250402,
    "USDAUD":1.304104,
    "USDAWG":1.79,
    "USDAZN":1.620604,
    "USDBAM":1.742204,
    "USDBBD":2,
    "USDBDT":78.430401,
    "USDBGN":1.752404,
    "USDBHD":0.377041,
    "USDBIF":1651,
    "USDBMD":1,
    "USDBND":1.362804,
    "USDBOB":6.860399,
    "USDZAR":13.710364,
    "USDZMK":5156.103593,
    "USDZMW":10.020363,
    "USDZWL":322.355011
  }
}

编辑:商店定义

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

        }
    ]
});

1 个答案:

答案 0 :(得分:0)

不,商店正在尝试获取您定义为

model
model : 'CurrencyConvert.model.CurrencyCode',

如果你这样做,你必须在你的代码库中有一个这样的定义:

Ext.define('CurrencyConvert.model.CurrencyCode',
    extend:'Ext.data.Model',
    ...
});

你必须加载该文件。在99%的情况下,模型文件将放在app/model/CurrencyCode.js中,应用程序的名称为CurrencyConvert,商店将包含requires部分

requires:[
    'CurrencyConvert.model.CurrencyCode'
]