[英]Calculated field from nested data
Is it possible to create some Ext.data.Field
, which would get its value from a nested data?是否可以创建一些
Ext.data.Field
,它会从嵌套数据中获取其值?
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.而此时我必须捕捉修改产品模型数据的事件并手动更新
User
总和字段。
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' }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.