如何从 ExtJs 4 中的商店访问嵌套模型 [英] How to access nested models from store in ExtJs 4
问题描述
我刚刚下载了 ExtJs 4 的最终版本,我正在尝试使用新的模型方法来实现一些东西.
I just downloaded the final version of ExtJs 4 and I'm trying to implement some things using new Model approach.
例如,我有一个名为 SetupModel 的模型,它有 2 个嵌套模型用户、报告.我创建新商店并设置商店的 Model 属性 = SetupModel.
For instance I have a model named SetupModel, it has 2 nested models Users, Reports. I create new store and I set the Model property of the store = SetupModel.
问题是 - 在数据加载到商店后,我如何访问我的嵌套属性?
The question is - how can I access my nested properties after data was loaded into the store?
我需要类似 myStore.data.Users() 的东西,但不正确.
I need something like myStore.data.Users(), but is incorrect.
有什么想法吗?
推荐答案
在定义模型时,您需要提供与嵌套模型的必要关联.因为,您没有提供您的代码.下面是一个例子:
When you define your model, you need to provide the necessary association with the nested models. Since, you have not provided your code. Here is an example:
我的产品型号:
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'
}
});
现在,如果您有一个包含产品的 User 模型实例.您可以通过以下方式访问产品:
Now, if you have a instance of User model with products. Here is how you can access the products:
var productStore = user.products();
注意 user.products()
返回一个 Ext.data.Store
.现在,您可以遍历或过滤或查找您的产品记录.以下是我获得第一个产品名称的方式:
Note that user.products()
returns a Ext.data.Store
. Now, you can traverse or filter or find your product record. Here is how I got my first product's name:
productStore.getAt(0).get('name');
这篇关于如何从 ExtJs 4 中的商店访问嵌套模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!