2

Ext.data.Fieldネストされたデータから値を取得するを作成することは可能ですか?

私はこれを試しましたが、うまくいきません:

Ext.define('User',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'id', type: 'int'},
    {name: 'sum', type: 'float', persist: false,
      convert: function(value, record) {
        return record.products().sum('cost');
      }}
  ],
  hasMany: 'Product'
});

Ext.define('Product',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'id', type: 'int'},
    {name: 'cost', type: 'float'}
  ]
});

1 回の応答でサーバーからデータを読み込みます。この時点で、製品モデルのデータを変更するイベントをキャッチし、User合計フィールドを手動で更新する必要があります。

4

1 に答える 1

0

これを試して:

Ext.define('Product',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'id', type: 'int'},
    {name: 'cost', type: 'float'}
  ]
});

Ext.define('User',{
  extend: 'Ext.data.Model',
  fields: [
    {name: 'id', type: 'int'},
    {name: 'sum',
      convert: function(value, record) {
        var sum = 0;
        Ext.each(record.products, function(cost){ sum += cost; } );
        return sum; 
      }}
  ],
  hasMany: { model : 'Product',  name : 'products' } 
});
于 2012-03-30T03:36:49.943 に答える