Roma Komarov on Nostr: Let's say I want to have a web component. Whenever I use it, I would like to provide ...
Let's say I want to have a web component.
Whenever I use it, I would like to provide a custom callback to run in its context, defining it in HTML.
Is there an idiomatic way to do so? The way I'm thinking of is to use `currentScript`:
<my-element>
<script> document.currentScript.myCallback = () => console.log('Hey!');</script>
</my-element>
And then call this `myCallback` from inside the `connectedCallback`.
Is there anything better (apart from using a unique `id` for this)?
Published at
2024-05-09 13:03:52Event JSON
{
"id": "1e48f19aac85cf6e2a10c1446c05e1498aa853b5f01b5c626ef1104270cc47f5",
"pubkey": "1ea5c2d0e935fb9fa76516688fbe2515d64704ee93ac3abf4198b15a125376e6",
"created_at": 1715259832,
"kind": 1,
"tags": [
[
"proxy",
"https://front-end.social/users/kizu/statuses/112411268359325836",
"activitypub"
]
],
"content": "Let's say I want to have a web component.\n\nWhenever I use it, I would like to provide a custom callback to run in its context, defining it in HTML.\n\nIs there an idiomatic way to do so? The way I'm thinking of is to use `currentScript`:\n\n \u003cmy-element\u003e\n \u003cscript\u003e document.currentScript.myCallback = () =\u003e console.log('Hey!');\u003c/script\u003e\n \u003c/my-element\u003e\n\nAnd then call this `myCallback` from inside the `connectedCallback`.\n\nIs there anything better (apart from using a unique `id` for this)?",
"sig": "985b487cf49198305c9dfe46153dc25539f5b32285b011a24f19b139546cc5d33d49b1e3f0db44dc3d2b33cac93a131ff26fef05c1bc281b1843f50d7a38cc05"
}