Page 1 of 1

Id field is hardcoded in model change event

Posted: Fri Mar 17, 2023 6:12 pm
by Josh Argent

Hi,

We are encountering some strange behaviour when we are assigning new ids to records in our tree grid. It seems that the root cause of this problem is the onModelChange handler in Store.js assumes the id fields is called id. In our implementation, we set Model.idField to be something different.

See this code snippet from onModelChange:

if ('id' in wasSet) {
  const { oldValue, value } = toSet.id;

  me.updateDependentRecordIds(oldValue, value);

  me.onRecordIdChange({ record, oldValue, value });
}

Should this be something like this, instead?

if (record.idField in wasSet) {
  const { oldValue, value } = toSet[record.idField];

  me.updateDependentRecordIds(oldValue, value);

  me.onRecordIdChange({ record, oldValue, value });
}

If we call onRecordIdChange manually when we assign the new id, everything works fine.

Thanks,
Josh


Re: Id field is hardcoded in model change event

Posted: Fri Mar 17, 2023 6:37 pm
by marcio

Hey Josh,

Thanks for the detailed report, I created a ticket to check/fix that https://github.com/bryntum/support/issues/6401


Re: Id field is hardcoded in model change event

Posted: Tue Mar 21, 2023 10:40 am
by Josh Argent

That's great, thanks Márcio!


Re: Id field is hardcoded in model change event

Posted: Tue Mar 21, 2023 11:15 am
by johan.isaksson

Hi Josh,

I have investigated this and suspect there might be a misunderstanding involved. When specifying an idField, you are changing the dataSource for the id field. On the record, it is still record.id. But when you assign to that, you are updating the underlying data based on the dataSource.

I tried the following scenario and it seems to work (it logs "idChange" to the console, and the underlying data object has its test property updated):

class MyModel extends Model {}
MyModel.idField = 'test';

store = new Store({
    modelClass : MyModel,
    data       : [
        { test : 1 }
    ]
});
        
store.on('idChange', () => console.log('idChange')); store.first.id = 2;

Hope that information is helpful! If not, please share some more details on what your code is doing that goes wrong.


Re: Id field is hardcoded in model change event

Posted: Tue Mar 21, 2023 3:51 pm
by Josh Argent

Hi Johan,

I think I've finally gotten to the bottom of what was happening!

The issue we were seeing was calling store.remove was not working for records which had had their id reassigned. Our code was reassigning the id using the dataSource rather than the id. So in your example we were doing: store.first.test = 2; which was not triggering the same id change events as doing store.first.id = 2;.

I've changed our code and works. Thanks for the clarification :)


Re: Id field is hardcoded in model change event

Posted: Tue Mar 21, 2023 3:53 pm
by johan.isaksson

Glad to hear it helped!