Model 从嵌套数据计算字段

Model 从嵌套数据计算字段,model,nested,field,extjs4,Model,Nested,Field,Extjs4,是否可以创建一些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.

是否可以创建一些
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'}
  ]
});
我在一个响应中从服务器加载数据。 此时,我必须捕获修改产品模型数据的事件,并手动更新
用户
总和字段。

尝试以下操作:

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' } 
});
试试这个:

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' } 
});