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

Stimulate without a reflex target #179

Closed bbugh closed 4 years ago

bbugh commented 4 years ago

Feature Request

Is your feature request related to a problem?

Turbolinks doesn't currently handle get requests from forms (https://github.com/turbolinks/turbolinks/issues/272). StimulusReflex handles this perfectly without the hacky workarounds, except that the stimulate JavaScript function requires a reflex target, but the Ruby side doesn't need to do anything except refresh the page.

In our specific case, have a get method search form. When the user clicks the search button, we use history.pushState to update the search query in the URL. Our users like to bookmark searches, so we need to be able to write real URLs instead of using session values.

We then call stimulate which rewrites the page. This works great, except in this case we've had to provide an empty Reflex target method.

class NilReflex < ApplicationReflex
  def nothing
  end
end
this.stimulate("NilReflex#nothing")

Describe the solution you'd like

stimulate can take no arguments and still work as expected, re-rendering the current controller action and view.

// Ideal
this.stimulate()

Additional context

Calling stimulate with no options currently raises this error:

Uncaught TypeError: Cannot read property 'split' of undefined
    at invokeLifecycleMethod (lifecycle.js:16)
    at HTMLDocument.<anonymous> (lifecycle.js:43)
    at dispatchLifecycleEvent (lifecycle.js:72)
    at Controller.stimulate (stimulus_reflex.js:114)
    at Controller._perform (project_search_controller.js:59)
    at invokeFunc (index.js:164)
    at trailingEdge (index.js:209)
    at timerExpired (index.js:197)
leastbad commented 4 years ago

I think we'd strongly consider a PR for this. I think it's a good idea, and I'm curious how you'd suggest implementing it.

Even if this feature doesn't come to pass, I would strongly suggest that we raise a better exception message if they call it with the wrong number of parameters.