开发者

Calculated field from nested data

开发者 https://www.devze.com 2023-03-04 06:31 出处:网络
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:

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 modifyin开发者_如何学Pythong 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' } 
});
0

精彩评论

暂无评论...
验证码 换一张
取 消