Inherit from ApplicationController without importing for each controller?
See original GitHub issueI know you can make use of JavaScript’s class inheritance to set up an “Application Controller” that will serve as the foundation for all of your controllers to build upon. How can you do this without calling import at the top of each file? After installing Stimulus in a fresh Rails app, you’re left with a app/javascript
file.
Storing controllers in app/javascript/all/controllers
and extending the Stimulus controllers from ApplicationController
usually would require an import
. Is there a way to put a base ApplicationController
in app/javascript/
that could be globally accessible in the Stimulus controllers without having to declare import at the top of each controller?
Before
import ApplicationController from "./application_controller";
export default class extends ApplicationController {
sayHi () {
super();
console.log("Hello from a Custom controller");
}
}
After
export default class extends ApplicationController {
sayHi () {
super();
console.log("Hello from a Custom controller");
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Inherit from ApplicationController without importing for each ...
Is there a way to put a base ApplicationController in app/javascript/ that could be globally accessible in the Stimulus controllers without ...
Read more >Class inheritance to set up an “Application Controller” without ...
How can you do this without calling import at the top of each file? Before import ApplicationController from "./application_controller"; export ...
Read more >Devise sign_in resource works even without importing module
Basic answer - the module Devise::Controllers::Helpers (which includes Devise::Controllers::SignInOut that you discovered) is automatically ...
Read more >Application Controller - Better StimulusJS
You can make use of JavaScript's class inheritance to set up an “Application Controller” that will serve as the foundation for all of...
Read more >How to autoload Stimulus within every controller file
You no longer need to import Controller manually within every Stimulus controller! Shout out to Konnor Rogers for showing me how to do...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s the ProvidePlugin config we use with Webpacker (ApplicationController is in
app/javascript/controllers/application_controller.js
):app/javascript
is a default load path so it should work out of the box. We’re using fairly stock Webpacker setup.