Premium support for our pure JavaScript UI components


Post 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


Post by marcio »

Hey Josh,

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

Best regards,
Márcio


Post by Josh Argent »

That's great, thanks Márcio!


Post 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.

Best regards,
Johan Isaksson

Post 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 :)


Post by johan.isaksson »

Glad to hear it helped!

Best regards,
Johan Isaksson

Post Reply