user49126
user49126

Reputation: 1863

Extjs adding a idproperty to fields defined in a store

I'm setting my fields directly in the store configuration.

Ext.define('T.store.Users', {
    extend: 'Ext.data.Store',

    autoLoad: false,

    fields: [
        { name: 'Id', type: 'int' },
        { name: 'Name', type: 'string' }
    ]
});

Is it possible to set somehow an idProperty for these fields direct in the store ? The only option I see is to create a separate model class containing a idProperty. But I'd like to avoid this.

Upvotes: 5

Views: 17573

Answers (3)

Doctor.Who.
Doctor.Who.

Reputation: 617

For ExtJS 6

Ext.define(null, {
    override: 'Ext.data.ProxyStore',

    /**
     * @cfg {String} idProperty
     */
    // idProperty: null,

    privates: {
        createImplicitModel: function(fields) {
            var me = this,
                modelCfg = {
                    extend: me.implicitModel,
                    statics: {
                        defaultProxy: 'memory'
                    }
                },
                proxy, model;
            if (fields) {
                modelCfg.fields = fields;
            }
            // add
            if(me.idProperty) {
                modelCfg.idProperty = me.idProperty;
            }

            model = Ext.define(null, modelCfg);
            me.setModel(model);
            proxy = me.getProxy();
            if (proxy) {
                model.setProxy(proxy);
            } else {
                me.setProxy(model.getProxy());
            }
        }
    }
});

Example

Ext.define('T.store.Users', {
    extend: 'Ext.data.Store',

    autoLoad: false,
    idProperty: 'Id',

    fields: [
        { name: 'Id', type: 'int' },
        { name: 'Name', type: 'string' }
    ]
});

Test

var store = Ext.create('T.store.Users');
console.log(store.model.idProperty); // "Id"
console.log(store.model.idField); // constructor {name: "Id", type: "int"..}
store.add({Id: '11', Name: 'XXX'})

Upvotes: 3

sra
sra

Reputation: 23973

The default id Property is id. You can change it either on the model or the reader of the proxy.

Note: the store can use the proxy of the model (not done in this example).

Example (with both)

// Set up a model to use in our Store
 Ext.define('User', {
     extend: 'Ext.data.Model',
     idProperty: 'Id',
     fields: [
         {name: 'firstName', type: 'string'},
         {name: 'lastName',  type: 'string'},
         {name: 'age',       type: 'int'},
         {name: 'eyeColor',  type: 'string'}
     ]
 });

 var myStore = Ext.create('Ext.data.Store', {
     model: 'User',
     proxy: {
         type: 'ajax',
         url: '/users.json',
         reader: {
             type: 'json',
             root: 'users',
             idProperty: 'Id'
         }
     },
     autoLoad: true
 });

Upvotes: 14

CD..
CD..

Reputation: 74156

You can theoretically change the idProperty from within the constructor this way:

Ext.define('T.store.Users', {
    extend: 'Ext.data.Store',

    autoLoad: false,

    constructor: function(){
        this.callParent(arguments);
        this.model.prototype.idProperty = 'Id';
    },

    fields: [
        { name: 'Id', type: 'int' },
        { name: 'Name', type: 'string' }
    ]
});

Upvotes: 5

Related Questions