Skip to content

Commit

Permalink
data.Model: fields => fields_, fieldsMap, updateFieldsMap() #6248
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiu committed Jan 16, 2025
1 parent 9cb060d commit 7891ea6
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/data/Model.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Model extends Base {
*/
ntype: 'model',
/**
* @member {Array|null} fields=null
* @member {Object[]|null} fields_=null
*/
fields: null,
fields_: null,
/**
* @member {String} keyProperty_='id'
*/
Expand All @@ -38,23 +38,47 @@ class Model extends Base {
trackModifiedFields: false
}

/**
* @member {Map} fieldsMap=new Map()
*/
fieldsMap = new Map()

/**
Triggered after the fields config got changed
* @param {Object[]|null} value
* @param {Object[]|null} oldValue
* @protected
*/
afterSetFields(value, oldValue) {
if (value) {
this.updateFieldsMap(value)
}
}

/**
* Finds a field config by a given field name
* @param {String} name
* @returns {Object|null} The field config object or null if no match was found
*/
getField(name) {
let me = this,
i = 0,
len = me.fields?.length || 0;

for (; i < len; i++) {
if (me.fields[i].name === name) {
return me.fields[i]
}
}
return this.fieldsMap.get(name) || null
}

/**
* @param {Object[]} fields
* @param {Boolean} isRoot=true
*/
updateFieldsMap(fields, isRoot=true) {
let {fieldsMap} = this;

isRoot && fieldsMap.clear();

fields.forEach(field => {
fieldsMap.set(field.name, field);

return null
// Assuming that nested fields contain the full path as the name, we do not need a prefix.
field.fields && this.updateFieldsMap(field.fields, false)
})
}
}

Expand Down

0 comments on commit 7891ea6

Please sign in to comment.