Fix initializers

This commit is contained in:
Andreas Nedbal 2023-01-06 08:35:02 +01:00 committed by Andreas Nedbal
parent ce067a2d57
commit f6d7d6ae2d
3 changed files with 23 additions and 23 deletions

View File

@ -1,10 +1,12 @@
import '@hotwired/turbo-rails'; import '@hotwired/turbo-rails';
import initialize from './initializers'; import initializeBootstrap from './initializers/bootstrap';
import initializeStimulus from './initializers/stimulus';
export default function start(): void { export default function start(): void {
try { try {
initialize(); initializeBootstrap();
initializeStimulus();
} catch (e) { } catch (e) {
// initialization errors // initialization errors
} }
} }

View File

@ -1,20 +0,0 @@
/**
* Using some JavaScript trickery with require.context
* and default exports, we're basically rebuilding the
* Rails concept of "initializers" in JavaScript.
*
* Every file in this folder exports a default function
* which this index script is loading and executing, so
* we don't have to specify several single import
* statements and can dynamically extend this with as
* many initializers as we see fit.
*/
export default function initialize(): void {
const files = require.context('.', false, /\.ts$/);
files.keys().forEach((key) => {
if (key === './index.ts') return;
if (key.startsWith('./_')) return;
files(key).default();
});
}

View File

@ -0,0 +1,18 @@
import { Application } from "@hotwired/stimulus";
import AnnouncementController from "retrospring/controllers/announcement_controller";
import AutofocusController from "retrospring/controllers/autofocus_controller";
import CharacterCountController from "retrospring/controllers/character_count_controller";
/**
* This module sets up Stimulus and our controllers
*
* TODO: Temporary solution until I implement stimulus-rails and move
* controllers to app/javascript/controllers where an automated
* index can be generated
*/
export default function (): void {
window['Stimulus'] = Application.start();
window['Stimulus'].register('announcement', AnnouncementController);
window['Stimulus'].register('autofocus', AutofocusController);
window['Stimulus'].register('character-count', CharacterCountController);
}