model association configuration

Models can have associations with other Models via Ext.data.association.HasOnebelongsTo and hasMany associations.

 1 Ext.define('IS.model.Grounp', {
 2             extend : 'Ext.data.Model',
 3             config : {
 4                 idProperty : 'GrounpID',
 5                 fields : [{
 6                             name : 'GrounpID',
 7                             type : 'string'
 8                         }, {
 9                             name : 'ReportName',
10                             type : 'string'
11                         }],
39                 hasMany : {
40                     model : 'IS.model.User',
41                     name : 'getUsers',//用recrod.data.getUsers可以获得对应的user数组,也可用model.getUsers()方法来获得
42                     associationKey : 'users'//在grounp.json中用来标志关联的user列表的跟节点名字  
                           //The name of the property in the data to read the association from. 
43 } 44 } 45 });
Ext.define('IS.model.User', {
            extend : 'Ext.data.Model',
            config : {
                idProperty : 'userId',
                fields : [{
                            name : 'userId',
                            type : 'string'
                        }, {
                            name : 'userName',
                            type : 'string'
                        }]
            }
        });

Define a store use Model Grounp:

Ext.define('IS.store.GrounpStore', {
            extend : 'Ext.data.Store',
            requires : ['IS.model.Grounp'],
            config : {
                model : 'IS.model.Grounp',
                autoLoad : false,
                proxy : {
                    type : 'ajax',
                    url : 'grounp.json',
                    reader : {
                        idProperty : 'GrounpID',
                        type : 'json',
                        rootProperty : 'grounps'
                    }
                }
            }
        });

grounp.json

 1 {
 2     grounps : [{
 3                 'GrounpID' : '20',
 4                 'GrounpName' : 'Team1',
 5                 'users' : [{
 6                             'userId' : '1',
 7                             'userName' : 'Felix'
 8                         }, {
 9                             'userId' : '4',
10                             'userName' : 'Colin'
11                         }]
12             }, {
13                 'GrounpID' : '23',
14                 'GrounpName' : 'Team2',
15                 'users' : [{
16                             'userId' : '2',
17                             'userName' : 'Ivy'
18                         }, {
19                             'userId' : '5',
20                             'userName' : 'Lucas'
21                         }]
22             }]
23 }

若list的对应store为GrounpStore,那么当itemtap event触发时

function(list, index, target, record, event) {
console.log(record.getUsers());获取相关users
}
原文地址:https://www.cnblogs.com/fengjian/p/2957857.html