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 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' }
});
精彩评论