Re: [whatwg/dom] Custom element callback/promise for "connected and parsed" (#662)

I've definitely wanted to have behaviour similar to [this issue](https://github.com/whatwg/html/issues/2235) for simple inline code. e.g. For this sort've use case:

```js
<math-plot>
  <module-script>
    export default function(x) {
      return x**2
    }
  </module-script>
</math-plot>
```

In this case it's the children of the `module-script` I would like to ensure only execute once (which can be important for use cases that fetch expensive resources).

I tried to create a customized builtin `<script>` element in Chrome Canary to simulate the behaviour but still the `connectedCallback` is executed too early.

<details>
<code><pre>
<script>
  class XModule extends HTMLScriptElement {
    connectedCallback() {
      console.log(this.text)
    }
  }
  
  customElements.define('x-module', XModule, { extends: 'script' })
</script>

<script is="x-module" type="_">
  console.log("Hello!")
</script>
</pre></code>
</details>

---

*On the other hand* this particular problem could be solved by ensuring that the `customElements.define` call always happens after parsing has completed.

e.g. Just defer the `customElements.define` call until `DOMContentLoaded`:

```js
class ScriptLike extends HTMLElement {
  // ...
}

if (document.readyState === 'interactive'
|| document.readyState === 'complete') {
  customElements.define('script-like', ScriptLike)
} else {
  document.addEventListener('DOMContentLoaded', _ => {
    customElements.define('script-like', ScriptLike)
  })
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/662#issuecomment-404054274

Received on Wednesday, 11 July 2018 06:00:33 UTC