Polymer and WebComponentsReady event

bitpshr picture bitpshr · Feb 13, 2014 · Viewed 17.1k times · Source

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.

Answer

user3267211 picture user3267211 · Feb 13, 2014

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.