Reputation: 2679
What patterns have been successful when mapping a model with a few repeating structures. I have to model and create a grid view a record with has a few columns which are basically the same repeating structure.
Ext.define('Ep.mm.model.MyComparison', {
var derived = function(val,)
extend: 'Ext.data.Model',
fields : [
{name:'id',type:'string'},
{name:'count',type:'number'},
{name:'special',type:'number'},
{name:'A_a',type:'number'},
{name:'A_b',type:'number'}
{name:'A_derived',renderer = function(val,meta,rec){return rec.get('A_b')-rec.get('A_a')}}
{name:'B_a',type:'number'},
{name:'B_b',type:'number'}
{name:'B_derived',renderer = function(val,meta,rec){return rec.get('B_b')-rec.get('B_a')}}
{name:'C_a',type:'number'},
{name:'C_b',type:'number'}
{name:'C_derived',renderer = function(val,meta,rec){return rec.get('C_b')-rec.get('C_a')}}
]
});
I am likely to end up with Server models/stores that differ only in the number of times this structure repeats (A) (A,B) (A,B,C,D). Is there a way to use a common model for each of A,B,C,D and just re-use the common model as the type. This is something akin to a 'nested' structure.
Ext.define('MyType',{
extend :'Ext.data.Model',
fields : [
{name : 'a', type:'number'},
{name : 'b', type:'number'},
{ name : 'derived',
convert : function(value, record) {
return record.get('a') - record.get('b');
}
}
]
}
Then MyComparison would be defined as
Ext.define('Ep.mm.model.MyComparison',{
extend : 'Ext'data.Model',
fields : [
{name:'A', type:'MyType'}
{name:'B', type:'MyType'}
{name:'C', type:'MyType'}
]
});
I know this isn't quite right, but this is sort of the functionality I am trying to create. Perhaps using the 'hasOne' association type?
Upvotes: 1
Views: 139
Reputation: 1521
I'd say you want functionality of a field type:
Ext.data.Types.A_DERIVED = {
convert: function(v, data) {
return rec.get('A_b')-rec.get('A_a');
},
type: 'number'
};
Then use it as a type of A_derived
field:
Ext.define('Ep.mm.model.MyComparison', {
extend: 'Ext.data.Model',
fields: [
{name:'id',type:'string'},
{name:'A_a',type:'number'},
{name:'A_b',type:'number'},
{name:'A_derived', type: 'Ext.data.Types.A_DERIVED'}
]
});
Afterthoughts:
- Maybe you would like to aggregate all A_*
data into one field? See documentation for an example.
- There is a trouble when you want to use the same mechanism with B_derived
because there is no way of supplying B
into convert
function. A dirty hack might be to set the defaultValue to B
and to bind the field to null data. Then v
would be read from defaultValue
.
Upvotes: 0
Reputation: 30082
Yes, you can extend your own model classes:
Ext.define('Base', {
extend: 'Ext.data.Model',
fields: ['name']
});
Ext.define('Child', {
extend: 'Base',
fields: ['age']
});
var o = new Child({
name: 'Foo',
age: 100
});
console.log(rec.get('name'), rec.get('age'));
As a sidenote, models don't have renderers. You're probably looking for the convert function on the field, here.
Upvotes: 1