Mehdi Fanai
Mehdi Fanai

Reputation: 4059

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

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

I need this store for a combobox like this but it wont work:

{
xtype : 'combo',
name : 'language',
fieldLabel : 'Language',
store : 'Language',
queryMode : 'local',
displayField : 'name',
valueField : 'id',
typeAhead : true,
forceSelection : true
}

Upvotes: 2

Views: 10448

Answers (3)

Molecular Man
Molecular Man

Reputation: 22386

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

Upvotes: 4

Molecular Man
Molecular Man

Reputation: 22386

For your example you have to modify your data to this:

data : [
    { id: "aa", 'name' : "Afar" },
    { id: "ab", 'name' : "Abkhazian" },
    ...
]

Upvotes: 2

Zango
Zango

Reputation: 2387

Firstly, in your combobox configuration you must have: store : Ext.create('MA.store.Language'), instead of: store : 'Language',

And secondly, your store definition must look like this:

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"
} ],

read : function() {
 var me = this; 
 var oldData = me.getProxy().data[0];
 var data = [];
 for (var prop in oldData) {
    if (oldData.hasOwnProperty(prop)) {
        data.push({
            id: prop,
            name: oldData[prop]
        });
    }
 } 
 me.loadData(data);
}

});
And it will work as you expect with your combobox.

EDIT: instead of this:

data.push({
    id: prop,
    name: oldData[prop]
  });
I had this:
data.push({
    id: prop,
    value: oldData[prop]
  });

Upvotes: 4

Related Questions