stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.28k stars 172 forks source link

Scoping when using Stimulus does not work as expected #144

Closed moveson closed 4 years ago

moveson commented 4 years ago

Feature Request

When using SR with Stimulus, it should be possible to use scoping to return a portion of the page even if there is only one Stimulus controller in use for the entire page.

Is your feature request related to a problem?

I wouldn't call this a bug exactly, but the behavior is unexpected. Say I have a large form with a lot of data, and I want to do server-side validations. I want a single Stimulus controller that will handle field mapping and submission to the validation reflex class, but I don't want to return all the html for the entire form with each validation.

The docs for Scoping indicate that the first place SR looks to determine a root is "Is there a data-reflex-root on the element with the data-reflex?"

But when using Stimulus to talk to the Reflex model, there is no element with a data-reflex to reference. So I am forced to set my root at the element containing the controller, which is at the very top of the form.

Describe the solution you'd like

I would like the logic that determines scoping to allow me to set a data-reflex-root on an html element that doesn't necessarily include a Stimulus controller or a data-reflex action.

leastbad commented 4 years ago

Hi @moveson! I don't think you're in the Discord channel... yet? 😁

I have one piece of bad news and two pieces of good news for you.

It's not likely that StimulusReflex will ever work the way you describe because it's a can of worms and a slippery slope towards a dozen what-if scenarios that would bloat the API and logic like a cancer if we tried to account for every scenario. You are, of course, free and encouraged to modify the library to fit your needs.

The first good news is that while I can't read your mind, I think that I understand what you're ultimately trying to accomplish and I wrote the gem you don't realize you're looking for: Optimism.

The second good news is that CableReady - the library upon which StimulusReflex is built - allows you to take direct control of your DOM elements and more via websockets. With clever use of CableReady, you can morph any element you wish along with 12 other methods including emiting DOM events.

moveson commented 4 years ago

@leastbad Thanks for the helpful response. The pattern I was using was the problem, and I've figured out a better solution.