Polymer: create a "generic" user element

I am trying to create an element with attributes that will indicate which element should consist of this element.

I have a flip element of a 3d map, which I use for this purpose in index.html, which is defined as:

<html>
    <head>
        <script src="bower_components/webcomponentsjs/webcomponents.js">
        </script>
        <link rel="import" href="elements/general-element.html">
    </head>
    <body>
        <general-element 
            name="card-flip" 
            address="elements/card-flip.html">
            <front>
                FRONT
            </front>
            <back>
                BACK
            </back>
        </general-element>
    </body>
</html>

It is indicated by general-element.html:

<link rel="import" 
    href="../bower_components/polymer/polymer.html">
<polymer-element name="general-element" attributes="name address">
    <template>
        <div id="element_here">
        </div>
    </template>
    <script>

      function addElementToDOM( element_name, _that ) {
          var elem = _that.shadowRoot.querySelector( "#element_here" );
          add_elem = document.createElement( element_name );
          elem.appendChild( add_elem );
      }

        Polymer ( 'general-element', {
          ready: function() {
              if ( 
                  ( this.address != undefined ) && 
                  ( this.name != undefined ) ) {

                  Polymer.import( 
                      [this.address], 
                      addElementToDOM( this.name, this )
                  );
              }
          }
        } );
    </script>
</polymer-element>

WARNING: the output is empty with an error in the console indicating that the attributes on the flip card were data-bound before updating the Polymer element. This can lead to incorrect binding types.

Can this be achieved in any way?

0
source share
1 answer

, , paper-button flip-card ( flip-card.)

TL; DR : http://plnkr.co/edit/yO865EkclZAsWBhzVSU4?p=preview

  • , addElementToDOM Polymer.import. function() { addElementToDOM(...); } .

  • (, front back), . , , .

  • , . .

  • , flip-card flip-card front back, content select='front/back' .

  • shadowRoot . getDistributedNodes, this.$.elementHere.

  • <body unresolved> polyfill, .

:

<!doctype html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <title>Add paper button</title>
  <script src="https://www.polymer-project.org/components/webcomponentsjs/webcomponents.js"></script>
  <link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import"> 
</head>

<body unresolved>
  <polymer-element name="general-element" attributes="name address">
    <template>
      <content select='my-front'></content>
      <div id="elementHere">
      </div>
      <content select='my-back'></content>
    </template>
    <script>
      Polymer ({
        addElementToDOM: function ( element_name ) {
          var elem = this.$.elementHere;
          add_elem = document.createElement( element_name );
          add_elem.appendChild(document.createTextNode('¡Hola!'));
          elem.appendChild( add_elem );
        },

        domReady: function() {
          if ( this.address && this.name ) {
            var self = this;
            Polymer.import( 
              [self.address], 
              function() { self.addElementToDOM( self.name ); }
            );
          }
        }
      });
    </script>
  </polymer-element>
  <general-element 
      name="paper-button" 
      address="https://www.polymer-project.org/components/paper-button/paper-button.html">
      <my-front>
          FRONT
      </my-front>
      <my-back>
          BACK
      </my-back>
  </general-element>
</body>
</html>

, , №1, . , .

P.S. ?

    <template>
      <content select='my-front'></content>
 <!-- ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ -->
      <flip-card></flip-card>
      <content select='my-back'></content>
    </template>
+4

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


All Articles