如何从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个嵌套模型Users,Reports。
我创建新的存储,并且我设置了store = SetupModel的Model属性。
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?
I需要像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'
}
});
现在,如果您有一个具有产品的用户模型实例。以下是如何访问产品:
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屋!