How to create Ext.data.Store from an unusual JSON store?

前端 未结 3 891
时光取名叫无心
时光取名叫无心 2021-01-15 00:38

I have this JSON store but it`s not coded correctly. What is the correct syntax for it?

Ext.define(\'MA.store.Language\', {
extend : \'Ext.data.Store\',
fiel         


        
3条回答
  •  庸人自扰
    2021-01-15 01:09

    So you want to use your unusual JSON anyway. In this case you can solve your problem by defining your own reader. Like this:

    Ext.define('MA.reader.Language', {
        extend: 'Ext.data.reader.Json',
        alias: 'reader.Language',
        read: function (response) {
            var data = [];
            for (var i in response[0])
                data.push({
                    id: i,
                    'name': response[0][i]
                });
            return this.callParent([data]);
        }
    });
    
    Ext.define('MA.store.Language', {
        extend: 'Ext.data.Store',
        fields: [{
            name: 'id'
        }, {
            name: 'name'
        }],
        data: [{
            "aa": "Afar",
            "ab": "Abkhazian",
            "ace": "Achinese",
            "ach": "Acoli",
            "ada": "Adangme",
            "ady": "Adyghe",
            "ae": "Avestan",
            "af": "Afrikaans",
            "afa": "Afro-Asiatic Language",
            "afh": "Afrihili",
            "ain": "Ainu",
            "ak": "Akan"
    
        }],
        proxy: {
            type: 'memory',
            reader: {
                type: 'Language'
            }
        }
    });
    
    var store = Ext.create('MA.store.Language', {
        storeId: 'Language'
    });
    
    var cc = Ext.widget('combo', {
        xtype: 'combo',
        name: 'language',
        fieldLabel: 'Language',
        store: 'Language',
        queryMode: 'local',
        displayField: 'name',
        valueField: 'id',
        typeAhead: true,
        forceSelection: true
    });
    
    cc.render(Ext.getBody());
    

    EDIT: working example

提交回复
热议问题