Extending Custom HTML Elements with Polymer

Hi, I would like to switch from my own HTML element using Polymer to create a custom web component. My polymer is ready to be called back when I am not expanding. As soon as I expand, nothing else gets called. Although creating a shadow DOM for the element ...

Here is my code to use:

<!DOCTYPE html>
<html>
    <head>
    <title>Custom Component Usage</title>
        <script src="bower_components/platform/platform.js"></script>
        <link rel="import" href="elements/extended-item.html">
    </head>
    <body>
        <extended-item>I was extended from div!</extended-item>
    </body>
</html>

A custom element that extends a div :

<link rel="import" href="../bower_components/polymer/polymer.html">

<polymer-element name="extended-item" extends="div">

    <template>

        <content></content>

    </template>

</polymer-element>

<script>
Polymer('extended-item',
        {
            created: function ()
            {
                console.log('EXTENDED ITEM IS CREATED!');
            },
            ready: function ()
            {
                console.log('EXTENDED ITEM IS READY!');
            },
            attached: function ()
            {
                console.log('EXTENDED ITEM IS ATTACHED!');
            },
            domReady: function ()
            {
                console.log('EXTENDED ITEMS DOM IS READY!');
            },
            detached: function ()
            {
                console.log('EXTENDED ITEM IS DETACHED!');
            },
            attributeChanged: function (attrName, oldVal, newVal)
            {
                //var newVal = this.getAttribute(attrName);
                console.log(attrName, 'old: ' + oldVal, 'new:', newVal);
            }
        });
</script>

Any idea?

thanks in advance Rob

+4
source share
1 answer

This works correctly if, instead of referring to an element <extended-item>, use instead <div is="extended-item">.

(jsbin):

<polymer-element name="extended-item" extends="div">
  <template>
    <p>This is part of the template.</p>
    <content></content>
  </template>

  <script>
    Polymer('extended-item', {
      created: function() {
        console.log('EXTENDED ITEM IS CREATED!');
      },
      ready: function() {
        console.log('EXTENDED ITEM IS READY!');
      },
      attached: function() {
        console.log('EXTENDED ITEM IS ATTACHED!');
      },
      domReady: function() {
        console.log('EXTENDED ITEMS DOM IS READY!');
      },
      detached: function() {
        console.log('EXTENDED ITEM IS DETACHED!');
      },
      attributeChanged: function (attrName, oldVal, newVal)
      {
        //var newVal = this.getAttribute(attrName);
        console.log(attrName, 'old: ' + oldVal, 'new:', newVal);
      }
    });
  </script>
</polymer-element>

<div is="extended-item">I was extended from div!</div>

EDIT. , , .

+8

Source: https://habr.com/ru/post/1547619/


All Articles