Polymer and WebComponentsReady event
Asked Answered
L

1

17

According to the Polymer docs, the WebComponentsReady event is necessary because...

The polyfills parse element definitions and handle their upgrade asynchronously. If you prematurely fetch the element from the DOM before it has a chance to upgrade, you’ll be working with an HTMLUnknownElement. In these situations, wait for the WebComponentsReady event before interacting with the element

I have an HTML page that imports a single web component and registers a handler that logs a statement when all web components are loaded:

<!DOCTYPE html>
<html>
    <head>
        <script src="bower_components/platform/platform.js"></script>
        <link rel="import" href="elements/my-element.html">
    </head>
    <body unresolved>
        <my-element></my-element>
        <script>
            window.addEventListener('WebComponentsReady', function(e) {
                console.log('components ready');
            });
        </script>
    </body>
</html>

Why is the WebComponentsReady event firing before my-element's ready polymer event? I need to know when I can interact with the custom element, e.g. change its properties and call its public methods.

Labrie answered 13/2, 2014 at 19:25 Comment(0)
R
23

In Polymer 1.0 you can just listen for WebComponentsReady.

In Polymer 0.5, because it does more things asynchronously, there's an extra event called polymer-ready which will fire when your elements are loaded. Here's a jsbin showing the order.

Racing answered 13/2, 2014 at 19:48 Comment(5)
Great, thanks for the information. Working perfectly.Labrie
Fwiw, WebComponentsReady is still fired by the CustomElements polyfill for the reasons given in the OP. This is useful when using CustomElements polyfill on it's own. Also fwiw, Polymer uses separate polymer-ready event mostly to support components using remote style-sheets without FOUC.Skyler
I have the same issue, but because I insert my custom elements after polymer-ready fired, I can't use it and I need to listen to individual elements ready event. Unfortunately these element's don't have a ready function at that point (e.g myElement.ready(doSomething()) ) because polyfills add these methods later. What can I do to solve this issue?Fullgrown
Only WebComponentsReady is raised, now.Bregma
To add on to what @Nikolay mentioned, Polymer 1.0 removed the polymer-ready event and now you only needs to listen to WebComponentsReadyParkins

© 2022 - 2024 — McMap. All rights reserved.