Skip to content

Commit

Permalink
data.Model: afterSetFields() => take ownership of creating & updating…
Browse files Browse the repository at this point in the history
… the record class #6251
  • Loading branch information
tobiu committed Jan 18, 2025
1 parent 4019756 commit d92cde9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
24 changes: 15 additions & 9 deletions src/data/Model.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Base from '../core/Base.mjs';
import Observable from '../core/Observable.mjs';
import Base from '../core/Base.mjs';
import RecordFactory from './RecordFactory.mjs';

/**
* @class Neo.data.Model
Expand Down Expand Up @@ -51,6 +51,14 @@ class Model extends Base {
*/
fieldsMap = new Map()

/**
* @param {Object} config
*/
construct(config) {
super.construct(config);
RecordFactory.createRecordClass(this)
}

/**
Triggered after the fields config got changed
* @param {Object[]|null} value
Expand All @@ -59,14 +67,12 @@ class Model extends Base {
*/
afterSetFields(value, oldValue) {
if (value) {
let me = this;

me.updateFieldsMap(value);
this.updateFieldsMap(value);

if (oldValue !== undefined) {
me.fire('fieldsChange', {
model: me
})
// Fields can get changed multiple times before the model instance is getting constructed.
// We only need the latest state before construction & honor run-time changes.
if (this.isConstructed) {
RecordFactory.createRecordClass(value)
}
}
}
Expand Down
16 changes: 1 addition & 15 deletions src/data/Store.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,7 @@ class Store extends Base {
*/
afterSetModel(value, oldValue) {
if (value) {
let me = this;

value.storeId = me.id;

RecordFactory.createRecordClass(value);

value.on('fieldsChange', me.onModelFieldsChange, me)
value.storeId = this.id
}
}

Expand Down Expand Up @@ -433,14 +427,6 @@ class Store extends Base {
}
}

/**
* @param {Object} data
* @protected
*/
onModelFieldsChange(data) {
RecordFactory.createRecordClass(data.model);
}

/**
* Gets triggered after changing the value of a record field.
* E.g. myRecord.foo = 'bar';
Expand Down

0 comments on commit d92cde9

Please sign in to comment.