15

I just downloaded the final version of ExtJs 4 and I'm trying to implement some things using new Model approach.

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 need something like myStore.data.Users(), but is incorrect.

Any thoughts?

4

1 に答える 1

22

モデルを定義するときは、ネストされたモデルとの必要な関連付けを提供する必要があります。それ以来、あなたはコードを提供していません。次に例を示します。

私の製品モデル:

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 モデルのインスタンスがあるとします。製品にアクセスする方法は次のとおりです。

var productStore = user.products();

user.products()を返すことに注意してくださいExt.data.Store。これで、製品レコードをトラバース、フィルタリング、または検索できます。最初の製品名を取得した方法は次のとおりです。

productStore.getAt(0).get('name');
于 2011-04-27T06:58:24.377 に答える