0

ExtJS MVC 機能を備えた ExtJS 4.0.7 を使用しています。hasMany私は親モデルを持っていbelongsToます。

子供たちにアクセスする正しい方法は何ですか? parent.children().data.items[0].data(とは対照的に)通過するparent.data.children[0]と、不要なプロパティがありますMyApp.model.parent_id。また、日付の保存方法の違いにも気付きました。

まず、親の定義を次に示します。したがって、親は多くの子供を持つことになります。

Ext.define('MyApp.model.Parent', {
    extend : 'Ext.data.Model',
    fields : [ 'id' ],
    hasMany : [ {
        model : 'MyApp.model.Child',
        name : 'children'
    }],
    proxy : {
        type : 'direct',
        api : {
            create : Parents.create,
            read : Parents.read,
            update : Parents.update,
            destroy : Parents.destroy
        }
    }
});

そして、それぞれの子は親に属します。

Ext.define('MyApp.model.Child', {
    extend : 'Ext.data.Model',
    fields : [ 'id', {
        name : 'validFrom',
        type : 'date',
        dateFormat : 'time'
        } ],
    belongsTo : 'Parent'
});

コントローラーに親をロードします。

this.getParentModel().load(id, {
    scope : this,
    success : function(parent) {
                    // the party looks good here when logged to console
                    console.log(parent);
        this.updateStore(parent);
    }
});

コンソールで親を調べると、次のことがわかります。

  1. console.log(parent.data.children[0]):

コンソール出力:

Object
id: 3
validFrom: -1767225600000
__proto__: Object
  1. console.log(parent.children().data.items[0].data):

コンソール出力:

Object
id: 3
MyApp.model.parent_id: 209 // why is that here?
validFrom: Thu Jan 01 1914 01:00:00 GMT+0100 (Central European Standard Time)
__proto__: Object

ここに画像の説明を入力

4

1 に答える 1

0

私が正しく理解していれば、あなたは次のことを望んでいると思います:

var someRecordId = 'idProperty';
var record = Ext.getStore('Parent').getById(someRecordId);

// this returns all hasMany for the name of of the property i.e. name: 'child'

record.child();  

これにより、親レコードの子のすべての項目データ (モデル定義に基づく) が返されます。これらのフィールドのみを子モデル定義に含める必要があります。したがって、parent_id は含まれません。Child のすべてのレコードを取得しようとしている場合は、他のストアと同じ方法でそれらを取得するだけです。

于 2013-06-24T22:13:43.243 に答える