Stimulus Discourse

How to discuss between two controllers?


#1

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?


#2

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


#3

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


#4

Thanks!

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


#5

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.