Skip to content

Commit

Permalink
Add documentation for importing Stimulus controllers from folders wit…
Browse files Browse the repository at this point in the history
…hout adding namespaces. Closes #15, closes #16
  • Loading branch information
excid3 committed Jun 2, 2023
1 parent 9040a4f commit 1bcc0f4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ controllers.forEach((controller) => {
})
```

#### Importing Stimulus controllers from parent folders (ViewComponents, etc)

To import Stimulus controllers from parents in other locations, create an `index.js` in the folder that registers controllers and import the `index.js` location.

For example, we can import Stimulus controller for ViewComponents by creating an `app/components/index.js` file and importing that in your main Stimulus controllers index.

```javascript
// app/javascript/controllers/index.js
import { application } from "./application"

// Import app/components/index.js
import "../../components"
```

```javascript
// app/components/index.js
import { application } from "../javascript/controllers/application"

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
application.register(controller.name, controller.module.default)
})
```

#### Import ActionCable channels:

```javascript
Expand Down

0 comments on commit 1bcc0f4

Please sign in to comment.