简体   繁体   中英

How do I call a Stimulus controller that's more than one word?

So I've tried

{ controller: "reset_form", action: "turbo:submit-end->reset_form#reset" } 
{ controller: "reset-form", action: "turbo:submit-end->reset-form#reset" } 
{ controller: "reset_form", action: "turbo:submit-end->reset-form#reset" } 
{ controller: "reset-form", action: "turbo:submit-end->reset_form#reset" } 

With reset_form_controller.js, reset-form-controller.js, reset-form_controller.js... 

Which all failed. Every single one-- I thought Stimulus just didn't work. Then I tried the base hello_controller.js example in my erb, and it did work. And yes, I did follow the hello_controller.js's guidelines(didn't work) and copied code LETTER FOR LETTER from hotwired.dev's showcase video(didn't work).

I ended up just changing it to reset_controller.js and doing

{ controller: "reset", action: "turbo:submit-end->reset#reset" } 

to get it to work. But I'm still curious-- why did everything else fail? I feel my sanity slipping away. Out of all the things to be absolutely defeated by, it's trying to get a stimulus controller that's more than one word to run.

As @Maxence comment, it works fine when we manually register stimulus controller, then there's something wrong with the automatically stimulus controllers registration>

It turns out the stimulus-rails gem replace '_' to '-' when it registers stimulus controllers automatically code

function registerControllerFromPath(path, under, application) {
  const name = path
    .replace(new RegExp(`^${under}/`), "")
    .replace("_controller", "")
    .replace(/\//g, "--")
    .replace(/_/g, "-") // <---- THIS

    ...

So i guess you could name your stimulus controller with _ like reset_form_controller.js and use it with the - like reset-form

{ controller: "reset-form", action: "turbo:submit-end->reset-form#reset" } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM