Lesson 2 — Wiring Alert dismissal
Alert already exists in Phlex::UI from Module 5 but its dismiss
button was a placeholder — it rendered the × but clicking it did
nothing. Now we wire it properly.
This is the simplest possible Stimulus integration — one controller, one action, no targets, no values — and it demonstrates the core pattern cleanly before we build more complex components.
The component
Update Components::Alert to add data-controller and wire the
dismiss button:
|
|
data-controller: "alert" tells Stimulus this element is managed by
the alert controller. data-action: "click->alert#dismiss" tells
Stimulus to call dismiss() on the alert controller when this button
is clicked.
As a quick test, add the following as the first line in the view_template for the
Views::Boards::Show view.
|
|
When you open go to the show page you should now be able to dismiss this alert. You can remove it when you’re convinced it’s working OK.
The Stimulus controller
|
|
this.element is always the element that has data-controller="alert"
on it — the root div of the alert. The controller fades it out then
removes it from the DOM entirely.
This is the complete pattern: Phlex generates the data-* attributes,
Stimulus attaches the behaviour. The component owns the wiring; the
controller owns the behaviour.