Stimulus Discourse

How to access data- elements from within a controller's children?


#1

How to access data- elements from within a controller’s children ? Example:

<div data-controller="parent" data-parent-foobar="foobar">
    <div data-target="parent.left" data-parent-foo="foo"></div>
    <div class="content"></div>
    <div data-target="parent.right" data-parent-bar="bar"></div>
</div>

this.data.get("foobar") works fine, but this.data.get("foo") and this.data.get("bar") return null.

Is this possible ? Or data- elements are supposed only to live within the controller’s element ?

What I’m doing right now is: this.leftTarget.dataset.parentFoo to access "foo", for example.


#2

Yes, that’s exactly how I’d do it.