Events triggered in a different order when cell has a validator

Tags: #<Tag:0x00007f51b9685c38> #<Tag:0x00007f51b9685ad0>

Hi, I have nuanced issue, with a hopefully simple solution I would like to get some help with. Let me start with some context:

We have a setup where we track changes to our grid rows, and we save the changes once the user leaves the current row. This can either happen by moving selection to the next row or, more importantly for my question, when the user clicks outside of handsontable.

We use afterChange hook to keep track of all the interesting changes, and afterDeselect hook to trigger the save call, and this has worked well for a long time.

Recently, we started incorporating validation and found that one of the consequences of declaring a validator for a particular cell type is that saving the cell becomes an asynchronous operation. This presents a problem for us because it changes the order in which afterDeselect and afterChange events are triggered - in application terms our trigger to save the changes comes before the application learns the changes have happened at all.

I’ve done some exploring in HoT codebase and came up with a workaround that seems to work: If there is an activeEditor present, I subscribe to postAfterValidate event and trigger the save call from within the callback. Otherwise I keep the old behaviour. This seems to work but discussing with the team, we are uncomfortable leaving this code in because it relies on an undocumented event (namely postAfterValidate).

Finally, I can ask my question: Do you have a recommended way to achieve saving the changes consistently after deselect, regardless of whether there is a cell currently being edited or not? Ideally, we want to only do it if the currently edited cell is valid at the moment (think allowInvalid prop is false).

If the answer is no, can you please add the postAfterValidate event (hook) to documentation so we have some confidence it is there to stay? Alternatively could you expose a completely new (but documented) event triggered around the same time as postAfterValidate?

Thanks,
Sandi

Hi @shrvic

Thank you for contacting us. Let me ask a question first, as I don’t see you mentioning trying one hook. Did you try to achieve that with the afterValidate hook? https://handsontable.com/docs/javascript-data-grid/api/hooks/#aftervalidate If I understand the requirements correctly it should be suitable for this case.

Hey @adrian.szymanski,

Thanks for reaching out, I had a very strong feeling I was looking at an AI response in the previous post :slight_smile:
The problem with using afterValidate is that that it gets executed just before the changes are committed (i.e. before done() is called in the highlighted lines here). It is precisely because of this that I ended up using postAfterValidate to implement the workaround.

Hi @shrvic

Thank you for the explanation. As this is a part of the internal API we don’t describe such methods in the documentation. The main reason why we keep them as private is the fact that they may change without any notice. However, you can always create a feature request to expose this method. You can post such request here: https://github.com/handsontable/handsontable/discussions