我设置了以下模型(简化):
Ext.define('UserGroup', {
extend: 'Ext.data.Model',
fields: ['GroupID', 'UserID'],
idProperty: '',
associations: [{
type: 'belongsTo',
model: 'User',
primaryKey: 'UserID',
foreignKey: 'UserID',
name: 'users'
}]
)};
Ext.define('User', {
extend: 'Ext.data.Model',
idProperty: 'UserID',
associations: [{
type: 'hasMany',
model: 'UserGroup',
primaryKey: 'UserID',
foreignKey: 'UserID',
name: 'userGroups'
}]
});
我已经为每个模型加载了现有商店,以及大量相关数据。
当我尝试从 User
访问 UserGroup
关联时,我没有收到任何数据。
Ext.getStore('User').getAt(12).userGroups().count(); // returns 0
我知道这是因为协会正在创建自己的商店。到目前为止,我只能通过
获取适当关联的数据Ext.getStore('User').getAt(12).userGroups().load();
它调用服务器来获取该信息,但随后我必须使用回调并等待响应。
那么,如何为 userGroups
关联指定要使用的存储,这样它就不会从服务器请求数据?
最佳答案
Afaik,有两种可能的解决方案:
- 覆盖
Ext.data.HasManyAssociation.createStore
。 将数据从原始存储传递到
Ext.data.HasManyAssociation.storeConfig
:Ext.define('User', { extend: 'Ext.data.Model', idProperty: 'UserID', associations: [{ type: 'hasMany', storeConfig: { data: originalStore.getRange() }, // ... }] });
关于javascript - 如何使 Ext JS 4 模型关联使用现有商店而不是创建自己的商店?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8053287/