Accessing tracked variables in VanillaJS is being discussed. NateDhaliwal is trying to add an event listener to an iframe, but it’s returning null. Alteras suggests using the Glimmer {{on}} modifier instead of working around DOM rendering and race conditions.
NateDhaliwal finds the suggestion helpful, but encounters a compile error when trying to implement it. Alteras corrects the mistake, and NateDhaliwal updates the code, but receives an ESLint error.
Arkshine resolves the issue by suggesting the import of the on modifier from @ember/modifier. pfaffman recommends looking at the Discourse source code, as well as all-the-plugins and all-the-themes repositories for learning purposes.
It changes the iframe which is in a DModal to include the code’s language and content. However, it tells me that iFrame is null, so it could not add addEventListener to it.
I’ve tried this:
Looks like you’re working in gjs, so you can just use glimmer functions instead of trying to work around DOM rendering and race conditions. Add the glimmer {{on}} modifier with the iframe element and make your on loaded function fire on it.
Hmm… it says that Error: 74:13 error 'on' is not defined in ESLint. And the docs have no mention of an import because it’s a modifier.
I added event as a parameter to onIframeLoaded because the docs did that.
Add import { on } from "@ember/modifier"; and this should work.
It’s a good idea to look at the Discourse source code. It’s a good place to learn with existing code.