How to discuss between two controllers?

I’d like to know how you discuss between two (or more) controllers?

I don’t speak about a parent and a child but two different controllers like we can with redux or whatever in React.

Do you have any example?

There about many pubsub solutions out there. Maybe you could describe your specific case in more detail?

I’ve created my own Custom Event Dispatcher. It’s heavily based on this article: https://atech.blog/atech/creating-a-simple-custom-event-system-in-javascript

1 Like

Thanks!

Yeah I used to have an Orchestrator when I did some Backbone.js. I’ll do the same here I think. :slight_smile:

If you’d like to access one controller from another, you can do the following:

import { Controller } from "stimulus"

export default class extends Controller {
  //...

  get otherController() {
    return this.application.getControllerForElementAndIdentifier(document.querySelector(".elementSelector"), "controller-name")
  }
}

This was discussed on Github.

2 Likes