Skip to main content

ExtJS4 adding a idproperty to fields defined in a store

Store defined:

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

    autoLoad: false,

    fields: [
        { name: 'Id', type: 'int' },
        { name: 'Name', type: 'string' }
    ]
});
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' }
    ]
});

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
 });

Comments