【问题标题】:Custom elements not detecting children when instantiated in template of another custom element在另一个自定义元素的模板中实例化时,自定义元素未检测到子元素
【发布时间】:2014-08-21 09:22:24
【问题描述】:

我有一个 RPG 主 HTML 元素,定义如下。我在 index.html 文件的主体中实例化它。

<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>

正如预期的那样,我还定义了网格布局和网格平铺元素。在网格布局的构造函数中,我试图引用它的子元素。但是该元素在上面的rpg-main类中实例化时认为它有0个孩子。

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;
  }
}

***以上代码打印“NUM CHILDREN: 0”

它只在我的 rpg-main 元素中被实例化,所以我很惊讶它不会将网格图块识别为子元素。可能是因为 grid-layout 元素是在 rpg-main 自定义元素的模板标签中实例化的吗? (所以也许 grid-layout 不认为它的孩子在'light dom'中?)这将是一种耻辱,但如果是这样,解决方法是什么?

【问题讨论】:

  • 这有点老了,在 JS 而不是 Dart 中,但它可能会对你有所帮助:github.com/cletusw/rpg
  • 嘿,这很酷。一般来说,您是如何为这类事情找到 Polymer 的?认为它可能适合游戏,因为它比以前的 html5 更像是面向对象的编程。
  • 到目前为止一切都很好。我认为如果我尝试扩展到真实游戏的规模和复杂性,就会出现性能问题。

标签: html dart polymer dart-polymer shadow-dom


【解决方案1】:

构造函数只是错误的地方。在使用它们之前,您需要让元素正确初始化。

试试

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

另见贾斯汀在这里回答When is shadowRoot available to a polymer component?

【讨论】:

  • 谢谢。还没有尝试过(我相信它会起作用)但我还有另一个问题。对不起,如果这是基本的 DOM 东西,但首先 - 是 void attach() 保留方法还是您自己在那里定义一个新方法?另外,对 super.attached() 的调用是否阻塞?
  • attached 是在PoylmerElement 中实现的方法,在所有初始化完成后由 Polymer 调用。您可以在元素中覆盖此方法以提供 attached 方法的自定义实现。如果你重写了这样的方法,你需要调用super.attached() 来执行默认的实现。如果您不调用super.attached(),则不会附加该元素。我不确定您所说的“阻止”是什么意思。它是同步执行的。在上面的例子中,printsuper.attached() 返回之后执行。
  • 我添加了@override 注释,以便更清楚地了解attached 是什么。
  • 非常感谢君特!是的,“阻塞”是指它是同步的还是异步的,但现在我明白了 attach() 是什么,我发现这个问题变得不那么有意义了。
  • 我刚刚看到and is called by Polymer after all initialization is done. 不正确。 attached 由 Polymer 调用以附加元素,您可以通过覆盖该方法来扩展/替换默认行为。反正我猜你理解正确。
猜你喜欢
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多