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.

bitpshr
  • 1,033
  • 2
  • 9
  • 21

1 Answers1

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.

Community
  • 1
  • 1
  • Great, thanks for the information. Working perfectly. – bitpshr Feb 13 '14 at 19:51
  • 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. – Scott Miles Feb 14 '14 at 08:07
  • 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? – Iman Mohamadi Feb 25 '15 at 10:36
  • 4
    Only WebComponentsReady is raised, now. – Nikolay Tsenkov May 29 '15 at 15:20
  • 2
    To add on to what @Nikolay mentioned, Polymer 1.0 [removed the `polymer-ready` event](https://www.polymer-project.org/1.0/docs/migration.html#polymer-ready) and now you only needs to listen to `WebComponentsReady` – rajsite Sep 20 '15 at 23:37