我刚刚下载了 ExtJs 4 的最终版本,我正在尝试使用新的模型方法来实现一些东西。
例如,我有一个名为 SetupModel 的模型,它有 2 个嵌套模型用户、报告。我创建新商店并设置商店的模型属性 = SetupModel。
问题是 - 数据加载到商店后如何访问我的嵌套属性?
我需要类似 myStore.data.Users() 的东西,但不正确。
有什么想法吗?
定义模型时,需要提供与嵌套模型的必要关联。因为,你没有提供你的代码。这是一个例子:
我的产品型号:
Product = Ext.define('Product',{
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'user_id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'price', type: 'float'}
],
proxy: {
type: 'localstorage',
id: 'products'
}
});
我的用户模型:
User = Ext.define('User',{
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'gender', type: 'string'},
{name: 'username', type: 'string'}
],
associations: [
{type: 'hasMany', model: 'Product', name: 'products'}
],
proxy: {
type: 'localstorage',
id : 'users'
}
});
现在,如果您有一个带有产品的用户模型实例。以下是访问产品的方法:
var productStore = user.products();
请注意,user.products()
返回一个Ext.data.Store
. 现在,您可以遍历或过滤或查找您的产品记录。这是我获得第一个产品名称的方式:
productStore.getAt(0).get('name');