Skip to content
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

Documentation for Outlets API #604

Merged
merged 2 commits into from Nov 21, 2022
Merged

Documentation for Outlets API #604

merged 2 commits into from Nov 21, 2022

Conversation

marcoroth
Copy link
Member

This Pull request adds documentation for the Outlets API which was introduced in #576.

It also updates the order for the other sections in the reference.

Feedback very welcome, especially on the used examples.

@marcoroth marcoroth mentioned this pull request Nov 18, 2022

## Attributes and Names

The `data-search-result-outlet` attribute is called an _outlet attribute_, and its value is a [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) which you can use to refer to other controller elements which should be available as outlets on the _host controller_.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The host controller designation may be introduced a bit sooner to help understanding this. Maybe on line 8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the terminology may already be decided upon but could we call this source controller instead of host controller?

So it's source/outlet not host/outlet?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed. I wasn't too happy with the term "host controller". It's not decided yet, that's just what I had in there as a placeholder. Happy to change it whatever makes to most sense. source seems better fitting to me

Copy link
Contributor

@lb- lb- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments below but what happens if you have an outlet that has two or more Controllers on it?

Maybe I missed something but does the name of the outlet have to match the name of the expected outlet controller?


## Attributes and Names

The `data-search-result-outlet` attribute is called an _outlet attribute_, and its value is a [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) which you can use to refer to other controller elements which should be available as outlets on the _host controller_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the terminology may already be decided upon but could we call this source controller instead of host controller?

So it's source/outlet not host/outlet?

docs/reference/outlets.md Outdated Show resolved Hide resolved
docs/reference/outlets.md Outdated Show resolved Hide resolved
docs/reference/outlets.md Outdated Show resolved Hide resolved

Outlet callbacks are specially named functions called by Stimulus to let you respond to whenever an outlet is added or removed from the page.

To observe outlet changes, define a function named `[name]OutletConnected()` or `[name]OutletDisconnected()`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To observe outlet changes, define a function named `[name]OutletConnected()` or `[name]OutletDisconnected()`.
To observe outlet changes, define a class method named `[name]OutletConnected()` or `[name]OutletDisconnected()`.

Not sure if needed but it may be clearer if we say that this is a class method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, in the targets section they are also called "methods" 👍🏼

docs/reference/outlets.md Outdated Show resolved Hide resolved
docs/reference/outlets.md Outdated Show resolved Hide resolved
docs/reference/outlets.md Outdated Show resolved Hide resolved

### Referencing non-controller elements

Stimulus will throw an exception if you try to declare an element as an outlet which doesn't have the corresponding `data-controller` and identifier on it:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this error be thrown?

Once the controller is instantiated? Or when you attempt to access the outlet?

Might be good to be clear here - for some cases the data controller attribute could be added later maybe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's throwing the error when you attempt to access the outlet, similar to how targets handle this. I'll try to make this clearer, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

docs/reference/outlets.md Outdated Show resolved Hide resolved
Co-authored-by: LB (Ben Johnston) <mail@lb.ee>
@marcoroth
Copy link
Member Author

Thanks for the review!

Some general comments below but what happens if you have an outlet that has two or more Controllers on it?

An element with two controllers attached to it also has two different controllers instances. You'll get the controller instance for the controller identifier you referenced as the outlet.

Maybe I missed something but does the name of the outlet have to match the name of the expected outlet controller?

Yes, the outlet name has to match the controller identifier. This is what's used to get right controller instance for a given element.
The reason why you are able to provide a CSS selector is that you can specify which element/instance you want to associate with the source controller, because usually there are more than one instance of the same controller on the page.

@lb-
Copy link
Contributor

lb- commented Nov 19, 2022

Thanks @marcoroth it makes sense that the outlet name must match the controller identifier. I must have missed that or maybe we can make it clearer.


## Definitions

Define controller identifiers in your controller class using the `static outlets` array. This array declares which other controller identifiers can be used as outlets on this controller:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Define controller identifiers in your controller class using the `static outlets` array. This array declares which other controller identifiers can be used as outlets on this controller:
Define controller identifiers in your controller class using the `static outlets` array. This array declares which other Controller identifiers can be used as outlets on this controller:

I see we say identifiers here. Just another nitpicky fix for proper noun usage of Controller

@dhh dhh merged commit 273f5a7 into hotwired:main Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants