1
votes

I want to create a generic Extjs store for combo boxes.

The store should be able to:

  1. Take a URL
  2. Read data using a proxy from the server
  3. Should work for any model

How can I achieve this?

This is what I have for start:

Ext.create('Ext.data.Store', {
    proxy: {
        type: 'ajax',
        url: rawURL,
        reader: {
            type: 'json'
        }
    }
})

Using ExtJS 4.1

1

1 Answers

1
votes
function getGenericStore(storeURL, valField, dispField) {
    var modelName = 'Model' + me.id;
    var model = Ext.define(modelName, {
        extend: 'Ext.data.Model',
        fields: [
            {name: valField, type: 'string'},
            {name: dispField, type: 'string'}
        ]
    });    
    return Ext.create('Ext.data.Store', {
        proxy: {
            type: 'ajax',
            model: model,
            url: storeURL,
            reader: {
                type: 'json',
                read: function(response) {
                    var json = Ext.decode(response.responseText);
                    var records = [];
                    Ext.each(json, function(item) {
                        var record = Ext.create(modelName);
                        record.set(val, item[val]);
                        record.set(display, item[display]);
                        records.push(record);
                    });

                    return {
                        success: true,
                        total: json.length,
                        count: records.length,
                        loaded: true,
                        records: records
                    };
                }
            }
        }
    });
}