question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inherit from ApplicationController without importing for each controller?

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
georgeclaghorncommented, Jun 18, 2020

Here’s the ProvidePlugin config we use with Webpacker (ApplicationController is in app/javascript/controllers/application_controller.js):

const { environment } = require('@rails/webpacker')
const webpack = require('webpack')

environment.plugins.append('Provide', new webpack.ProvidePlugin({
  ApplicationController: ['application_controller', 'default']
}))
1reaction
javancommented, Jun 19, 2020

app/javascript is a default load path so it should work out of the box. We’re using fairly stock Webpacker setup.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found