Stimulus: Wait for controllers to initialize

Created on 2 Nov 2018  路  3Comments  路  Source: hotwired/stimulus

Loving it so far, good job. Sorry if this is not the right place for this question.


In my navigation controller I'm trying to use this.application.getControllerForElementAndIdentifier during initialize (can't move to click, third party) to obtain controllers that contains behavior I need to attach to my the navigation solution (close stuff, hide stuff, etc), but (I think) because the navigation occurs first in the HTML, during its initialize, other controllers still don't exist, so I can't get an instance of them.

stimulus is in a constant state of connecting and disconnecting (which is very nice), and I think it doesn't emit any kind of everything ready (but this for first load would be very useful I think).

What is the proper way to approach this problem?

Most helpful comment

here are two discussions that can probably help you

The idea is to put your code into a promise so that it gets evaluated within the next microtask

Promise.resolve().then(() => {
  // Stimulus has responded to the change by now
})

All 3 comments

here are two discussions that can probably help you

The idea is to put your code into a promise so that it gets evaluated within the next microtask

Promise.resolve().then(() => {
  // Stimulus has responded to the change by now
})

Perfect. It solves the problem.

Why not an API to that? Like application.tick() or something?

This has come up a few times before (#35, #183).

Why not an API to that? Like application.tick() or something?

It's the wrong level of abstraction for Stimulus. You shouldn't have to know about the microtask queue or JavaScript event loop in order to link two controllers together. Nor should you have to call the getControllerForElementAndIdentifier method (which has an intentionally long-winded name to discourage its use).

What we want is a way to declaratively link two controllers together in HTML, plus some sort of callback that's invoked when the link is established and both controllers are initialized.

We would love to hear any proposals for such an API! Otherwise, discussion for how to work with the existing API is best suited for the community forum.

Thank you for opening the issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chocnut picture chocnut  路  4Comments

JanaDeppe picture JanaDeppe  路  4Comments

thooams picture thooams  路  4Comments

joeybeninghove picture joeybeninghove  路  4Comments

merwok picture merwok  路  4Comments