Hotwire Discussion

Stimulus action on tbody when children are changed


I have a table that updates with new rows from a stimulus controller table-update. When this update has taken place i would like to update some other elements on the page.

The first part was simple enough, generate the html serverside and replace all the children of the tbody. On the tbody i would have thought that i should have an action like change->update-select#update. This does not work.

I suspect that whatever needs to be done might be easy but im not even sure where to start looking for this.

Is this a global event as described in the handbook, if yes which event is it?
IIs it even possible to detect such a dom change and fire a stimulus controller? If yes, what im i missing, some sort of.event listener I presume, but i can’t find any trace of how to solve this.

I have considered using change callbacks such that table-update controller sets data-table-update-updated-value on the tbody and the have the actual callback handler in the update-select controller, im obviously also failing to get that to work as the data-value is bound one or the other controller as I understand it. Is this approach feasible at all?

My update-select controller wraps both the select and tbody so it should be in scope. I can post it if needed, but as it might be clear, it does nothing besides connecting to the DOM currently. Im sure that there is a dead easy Turbo solution, unfortunately that is not available in my toolbox, stimulus 2.0 is.

Thanks in advance

Look in to MutationObservers - they are pretty straight forward and used under the bonnet by Turbo.

Thanks. With this knowledge in hand I found GitHub - stimulus-use/stimulus-use: A collection of composable behaviors for your Stimulus Controllers</ti. I all fairness I will go back to see if I can upgrade to Turbo even though we have decided to wait to Rails-7 for that. I seems like that I’m building something Turbo-ish just with less skill and knowledge.

Thanks for taking the time to answer.

Turbo/Turbo Streams/Turbo Frames are great and it would be worth anyones while getting to grips with the fundamentals, but, as it’s still evolving, committing to using it in a large project right now might result in a some refactoring later.

I’m working with it in a proof of concept project so took that as an opportunity to learn it. I’ve had to adapt things a couple of times as new functionality made some of the things I’d implemented obsolete :smiley: