New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for controllers to initialize #201

Closed
incompletude opened this Issue Nov 2, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@incompletude
Copy link

incompletude commented Nov 2, 2018

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?

@adrienpoly

This comment has been minimized.

Copy link

adrienpoly commented Nov 2, 2018

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
})
@incompletude

This comment has been minimized.

Copy link
Author

incompletude commented Nov 2, 2018

Perfect. It solves the problem.

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

@sstephenson

This comment has been minimized.

Copy link
Contributor

sstephenson commented Nov 2, 2018

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!

@sstephenson sstephenson closed this Nov 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment