简体   繁体   中英

Calculated field from nested data

Is it possible to create some Ext.data.Field , which would get its value from a nested data?

I have tried this, but it doesn't work:

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

I load data from server in a single response. And at this moment I have to catch event of modifying data of Product model and manually update User sum field.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM