A spreadsheet like controller for a table with two controllers with shared target

Hey salex! I actually just responded to your other post. This site is definitely low traffic, but perhaps we can help change that.

Thanks for all of the code and especially the screenshot, really helps me visualize what’s going on.

Is the main problem you’re trying to solve for how to trigger the update in the scoreTeam controller without looking it up and triggering a click from scoreMate? I agree it’s a bit of a kludge now, but certainly not the worst thing in the world. Here are a few ideas that should be a bit cleaner.

1. Dispatch update events
Within scoreMateController#fireCompute, you can dispatch a custom event that is listened to by scoreTeamController and triggers the update. I won’t go into this as I don’t think it’s the correct solution here, but it’s a good pattern for decoupling that deserves some attention. I give some examples here.

2. Listen for changes directly from scoreTeamController
Stimulus controllers can overlap, which can prove extremely useful. You already have yours setup as such, where your targets are assigned to both scoreMateController and scoreTeamController. As such, you can update your cells to trigger two actions, as such:

td = select_tag("mate[#{m}][front]", options_for_select(opt[:side],'Select'),
    data:{target:'scoreMate.mateFront scoreTeam.mateFront',
    action:"change->scoreMate#front change->scoreTeam#compute"},
    class:" w3-select")

And with that, remove scoreMateController#fireCompute altogether.

I didn’t take the time to fully understand every step of the code, so it’s possible I’m missing something (e.g. a race condition in computing values). The events do seem to trigger in order, so if scoreMate does need to compute first, just make sure that action is listed first and you should be fine.

Hope this helps!