Context: My JSON contains an array and I have to "flatten" it to put data in the form view.
The line in blue is how I do it:
JSON received from server containing an array
Code:
names: [{
name: "first",
value: "John"
},{
name: "last",
value: "DOE"
}],
My Form panel
Code:
items: [
{
xtype: 'fieldset',
items: [
{
xtype: 'textfield',
label: 'Field',
name: 'first_name'
}
]
}
My Model
Code:
Ext.define('Test.model.Person', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field',
'Ext.data.association.HasMany'
],
uses: [
'Test.model.Name'
],
config: {
fields: [
{
mapping: 'names.filter(function(obj){return obj.name == "first"; })[0].value;',
name: 'first_name',
type: 'string'
}
],
hasMany: {
model: 'Test.model.Name'
}
}
});
IT WORKS: the form is displayed with the `John` value in the `first_name` textfield. However my question is: it is the right way to maps arrays to forms? Or is there a better way?