Skip to content

Commit

Permalink
data.RecordFactory: createField() => re-add support for nested fields #…
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiu committed Jan 18, 2025
1 parent 2890a0c commit 71f64b5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
14 changes: 7 additions & 7 deletions examples/table/nestedRecordFields/EditUserDialog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ class EditUserDialog extends Dialog {
await me.timeout(20);

me.getItem('country-field') .value = record.country;
me.getItem('firstname-field').value = record.user.firstname;
me.getItem('lastname-field') .value = record.user.lastname;
me.getItem('selected-field') .checked = record.annotations.selected
me.getItem('firstname-field').value = record['user.firstname'];
me.getItem('lastname-field') .value = record['user.lastname'];
me.getItem('selected-field') .checked = record['annotations.selected'];
}
}

Expand All @@ -106,14 +106,14 @@ class EditUserDialog extends Dialog {
* @param {Object} data
*/
onFirstnameFieldChange(data) {
this.record.user.firstname = data.value
this.record['user.firstname'] = data.value
}

/**
* @param {Object} data
*/
onLastnameFieldChange(data) {
this.record.user.lastname = data.value
this.record['user.lastname'] = data.value
}

/**
Expand All @@ -124,11 +124,11 @@ class EditUserDialog extends Dialog {
store = me.getStateProvider().getStore('mainStore');

if (data.value === false) {
me.record.annotations.selected = false
me.record['annotations.selected'] = false
} else {
// Assuming we want to support a single row selection
store.items.forEach(record => {
record.annotations.selected = record === me.record ? data.value : false
record['annotations.selected'] = record === me.record ? data.value : false
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MainStore from './MainStore.mjs';
import StateProvider from '../../../src/state/Provider.mjs';
import Store from '../../../src/data/Store.mjs';

const countrySymbol = Symbol.for('country');
const dataSymbol = Symbol.for('data');

/**
* @class Neo.examples.table.nestedRecordFields.MainContainerStateProvider
Expand Down Expand Up @@ -51,7 +51,7 @@ class MainContainerStateProvider extends StateProvider {
country = record.country;

// hack resetting the current value to get a new record change
record[countrySymbol] = null;
record[dataSymbol].country = null;

record.country = country
})
Expand Down
6 changes: 3 additions & 3 deletions examples/table/nestedRecordFields/MainModel.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MainModel extends Model {
type: 'Object',

fields: [{
name : 'annotations.selected',
name : 'selected',
type : 'Boolean',
defaultValue: false
}]
Expand All @@ -28,10 +28,10 @@ class MainModel extends Model {
type: 'Object',

fields: [{
name: 'user.firstname',
name: 'firstname',
type: 'String'
}, {
name: 'user.lastname',
name: 'lastname',
type: 'String'
}]
}]
Expand Down
33 changes: 18 additions & 15 deletions src/data/RecordFactory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,35 @@ class RecordFactory extends Base {
* @param {String} data.path=''
*/
createField({field, me, model, path=''}) {
let fieldName = field.name.split('.').pop(),
fieldPath, properties,
value;
let fieldName = field.name,
fieldPath = path === '' ? fieldName : `${path}.${fieldName}`,
properties;

if (field.fields) {
field.fields.forEach(childField => {
fieldPath = path.split('.');
fieldPath = fieldPath.filter(Boolean);
fieldPath.push(field.name);

this.createField({field: childField, me, model, path: fieldPath.join('.')})
this.createField({field: childField, me, model, path: fieldPath})
})
} else {
if (value === undefined && Object.hasOwn(field, 'defaultValue')) {
value = field.defaultValue
}

properties = {
[fieldName]: {
[fieldPath]: {
configurable: true,
enumerable : true,
get() {
if (model.hasNestedFields) {
return Neo.ns(fieldPath, false, this[dataSymbol])
}

return this[dataSymbol][fieldName]
},
set(value) {
let oldValue = this[dataSymbol][fieldName];
let me = this,
oldValue;

if (model.hasNestedFields) {
oldValue = Neo.ns(fieldPath, false, me[dataSymbol])
} else {
oldValue = me[dataSymbol][fieldName]
}

value = instance.parseRecordValue(me, field, value);

Expand All @@ -95,7 +98,7 @@ class RecordFactory extends Base {
}
}

Object.defineProperties(path ? Neo.ns(path, true, me) : me, properties)
Object.defineProperties(me, properties)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/grid/View.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class GridView extends Component {
cellCls = ['neo-grid-cell'],
colspan = record[me.colspanField],
{dataField} = column,
fieldValue = Neo.ns(dataField, false, record),
fieldValue = record[dataField],
cellConfig, rendererOutput;

if (fieldValue === null || fieldValue === undefined) {
Expand Down Expand Up @@ -495,7 +495,7 @@ class GridView extends Component {
trCls.push('neo-even')
}

if (selectedRows && Neo.ns(me.selectedRecordField, false, record)) {
if (selectedRows && record[me.selectedRecordField]) {
NeoArray.add(selectedRows, id)
}

Expand Down
4 changes: 2 additions & 2 deletions src/table/View.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class View extends Component {
cellCls = ['neo-table-cell'],
colspan = record[me.colspanField],
{dataField} = column,
fieldValue = Neo.ns(dataField, false, record),
fieldValue = record[dataField],
hasStore = tableContainer.store?.model, // todo: remove as soon as all tables use stores (examples table)
{vdom} = me,
cellConfig, rendererOutput;
Expand Down Expand Up @@ -206,7 +206,7 @@ class View extends Component {

me.recordVnodeMap[id] = rowIndex;

if (selectedRows && Neo.ns(me.selectedRecordField, false, record)) {
if (selectedRows && record[me.selectedRecordField]) {
NeoArray.add(selectedRows, id)
}

Expand Down

0 comments on commit 71f64b5

Please sign in to comment.