Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extjs adding a idproperty to fields defined in a store

Tags:

extjs

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.

like image 665
user49126 Avatar asked Feb 19 '13 09:02

user49126


1 Answers

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
 });
like image 146
sra Avatar answered Oct 28 '22 18:10

sra