Custom elements not detecting children when instantiated in template of another custom element

a 夏天 提交于 2019-12-08 06:35:11

问题


I have an RPG Main HTML Element defined as follows. I'm instantiating it in the body of my index.html file.

<link rel="import" href="packages/polymer/polymer.html">
<link rel="import" href="gridlayout.html">
 <link rel="import" href="gridtile.html">


<polymer-element name="rpg-main" attributes="">
  <template>
    <style>
      grid-tile {
      background: #FF00FF;
      width: 50px; 
      height: 50px     
      }
     :host {
       position: absolute;
       display: block;
       width: 500px;
       height: 500px;
      }

    </style>
    <grid-layout rows = "2" cols = "2" spacing = "50px">
       <grid-tile>
       </grid-tile>
       <grid-tile>
       </grid-tile>
       <grid-tile>
       </grid-tile>
       <grid-tile>
       </grid-tile>
    </grid-layout>
  </template>
  <script type="application/dart" src="rpgmain.dart"></script>
</polymer-element>

As one would expect, I also have grid-layout and grid-tile elements defined. In the constructor for the grid-layout, I am trying to reference its children. But the element thinks it has 0 children when it is instantiated in the above rpg-main class.

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('grid-layout')
class GridLayout extends PolymerElement {
  @published int rows;
  @published int cols;
  @published int spacing;

  String _px = "px";
  String _perc = "%";

  GridLayout.created() : super.created() {

    print("NUM CHILDREN " + this.children.length.toString());

   /** for (int i = 0; i <= this.rows + this.cols - 2; i++) {
      Element child = this.children[i];
      this.children[i].style.margin = this._parseNum(spacing);
      child.style.float = "right";

      if (i % this.rows == this.rows) {
        child.style.clear = "right";
      }


    }**/
  }
  num _parseString(String s) {
    print(s.toString());
    return num.parse(s.split(_px)[0]);
  }

  String _parseNum(num n) {
    return n.toString() + _px;
  }
}

***The above code prints "NUM CHILDREN: 0"

It's only being instantiated in my rpg-main element, so I'm surprised it wouldn't recognize the grid-tiles as children. Could it be because the grid-layout element is being instantiated in the template tag of an rpg-main custom element? (So maybe grid-layout doesn't consider its children to be in the 'light dom'?) That would be a shame, but if so, what would the workaround be?


回答1:


The constructor is just the wrong place. You need to allow the elements to properly initialize before using them.

Try

@override
void attached() {
  super.attached();
  print("NUM CHILDREN " + this.children.length.toString());
}

see also Justins answer here When is shadowRoot available to a polymer component?



来源:https://stackoverflow.com/questions/24499713/custom-elements-not-detecting-children-when-instantiated-in-template-of-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!