【问题标题】:Construction and ordering of nested and template if= style polymer dart elements?嵌套和模板 if= 样式聚合物飞镖元素的构造和排序?
【发布时间】:2014-05-02 14:39:10
【问题描述】:

用于说明这些多个构造函数调用的非常小的应用程序的代码现已在 GitHub 上的https://github.com/Chiba-City/testnest 上发布。

我在想办法

  1. 为什么聚合物元素会构造两次
  2. 什么决定了包含其他聚合物元素的聚合物元素的构造顺序以及
  3. 为什么要使用“if”模板构建“模板化”元素。

另外,如果不能用三个简单的聚合物元素(一个定义为包含或嵌套另外两个)重现这些结果,我也很想知道这一点。

我发现 重复的构造函数调用 和构造函数排序特性。以下是对这些特殊性发生的解释。

我有一个非常简单的“应用程序”。有一个 index.html 文件,其中 html 包含一个自定义元素定义文件,并在其主体中仅声明一个自定义聚合物飞镖元素。以下是 index.html 文件的相关部分。我省略了惯用的样板。

<!-- index.html -->
<!-- in head -->
<link rel="import" href="icapp.html">
<!-- boiler plate polymer/init.dart and ../dart.js stuff here -->

<!-- only contents of body tag -->
<icount-app></icount-app>

现在在“icapp.html”中,我定义了两个仅由 h3 标签组成的元素。我在模板中包含这两个元素,第三个元素以简单的 H1 标签开头。这是所有三个元素的代码。

<!-- icapp.html -->
<!-- User stats list -->
<polymer-element name="icount-statlist" >
  <template><h3>My Stats</h3></template>
</polymer-element>

<!-- Find stats -->
<polymer-element name="icount-findstats" >
  <template><h3>Find New Stats</h3></template>
</polymer-element>

<!-- This is the element that contains the other two elements in its template -->
<polymer-element name="icount-app"> 
<template>
  <h1>icount.us</h1> <!-- a simple h1 heading -->

  <!-- Included always -->
  <icount-findstats></icount-findstats>

  <!-- Included conditionally - but can never be true -->
  <template if="{{false}}">
    <icount-statlist ></icount-statlist>
  </template>
<!-- End icount-app template -->
</template>
<!-- The code with the element definitions -->
<script type="application/dart" src="icapp.dart"></script>
</polymer-element>

我在“.created() : super.created”构造函数中放置了“print”语句,并覆盖了“enteredView()”方法来确定这些调用的顺序和频率。这是执行期间的输出。我添加了以“--”为前缀的注释

-- note this is first, although conditional on {{false}}
IcountStatlist: created           
-- now the containing element is constructed
IcountApp: created                
-- notice that enteredView happens before the nested element
IcountApp: enteredView
-- notice that this element is constructed twice
IcountFindstats: created          
IcountFindstats: created
IcountFindstats: enteredView

应用程序的输出符合预期(H1 和 H3 标记,后者用于无条件包含的“icount-findstats”元素)。但它是如何到达这里的却是奇特的,而且似乎无法为这些标签(属性、事件处理程序等)添加合理的程序逻辑。

  1. 可见的嵌套元素被构造了两次
  2. “模板化”嵌套元素构建一次
  3. 调用可见嵌套元素的“enteredView”方法(不足为奇)。

在包含的可见元素之上构造包含元素之后。但简单的实验表明,这种排序似乎是任意的,或者至少不依赖于嵌套元素的模板或排序。

如果我们简单地切换我们“模板化”的嵌套元素的偏好,我们会得到以下输出。

IcountStatlist: created
IcountStatlist: created
IcountStatlist: enteredView
IcountApp: created
IcountApp: enteredView
IcountFindstats: created

这里我们可以注意到:

  1. 可见的嵌套元素,这次是 icount-statlist,像上面一样被构造了两次,但在 之前 包含元素。此外,HTML 输出 h3 标记仅出现一次。

  2. 此可见嵌套元素的“enteredView”方法在包含元素之前调用

  3. “模板化”嵌套元素再次创建(仅一次),但现在包含元素之后。

进一步的实验表明,相同的元素无条件地、可见地嵌套 两次 会导致 4 次构造函数调用(针对该元素)。

另一个实验表明,有条件地包含两次相同的元素会导致 2 次构造函数调用(针对该元素)。

对“模板化”元素和可见元素重新排序的实验表明日志输出没有变化,即构造函数调用的顺序或频率。

不用说,我很困惑。

  • 为什么可见嵌套元素构造两次

  • 为什么要构造“模板化”嵌套元素根本

  • 为什么一个嵌套元素在 before 包含元素而另一个 after 构造?

  • 为什么 包含 元素 icount-app 本身没有构造两次?

  • 根据自定义聚合物元素(对象)构造的性质和顺序,包含元素如何合理地设置属性所包含元素?

    李>

非常感谢任何帮助。

【问题讨论】:

  • 在 GitHub 上发布整个项目怎么样?重建一个可以重现的项目需要做很多工作,而且有很多可能会有所不同,这使得讨论变得困难。
  • 这里是公开git repo - https://github.com/Chiba-City/testnest的代码 这是我第一次推Dart项目。如果您有任何问题,请告诉我。非常感谢您的帮助。这是一个小项目,仅用 print() 语句说明异常

标签: dart dart-polymer polymer


【解决方案1】:

注意

  • enteredView 改为attached
  • leftViewdetached

-------

我尝试了您的代码,但无法重现您提到的内容。

这是我运行您的应用时得到的输出:

nest-a: created
nest-a: enterView
nest-container: created
nest-container: enteredView
nest-b: created
nest-b: enteredView
  • 嵌套的nest-b根本没有实例化(输出来自&lt;template if="{{false}}"&gt;之外的nest-b

false 更改为true 后,我得到了

nest-a: created
nest-a: enterView
nest-container: created
nest-container: enteredView
nest-b: created
nest-b: enteredView
nest-b: created
nest-b: enteredView

您需要将值放在引号 ="{{false}}" 而不是 ={{false}} 无论如何它都按预期工作,因为这也被评估为 false,但对于所有其他值/表达式都会失败

  • 我不知道施工订单是否已解决。 最近还在讨论是应该由内而外构建还是文件顺序。 如果您使用绑定,这通常不是问题

您能否将您发布的应用程序的输出发布到 GitHub 存储库?

【讨论】:

  • 所以你是说解决了?我猜一个稳定的 1.3 很快就会发布(虽然不能说多久)。
  • Gunter,是的,多个构造函数调用问题消失了!太神奇了,也许假设没有人使用稳定版本? :) 祈祷现在我可以开始编写一些“真正的代码”了。大声笑...再次感谢!
  • 如果您能通过点击投票图标下方的复选标记来接受我的回答,那就太好了。
【解决方案2】:

此问题出现在 Dart Editor 的以下稳定版本中。

$ dart --version 
Dart VM version: 1.2.0 (Tue Feb 25 07:34:09 2014) on "linux_x64"

安装最新的 dev channel 版本的 Dart SDK 使得多个构造函数调用消失了。

$ dart --version
Dart VM version: 1.3.0-dev.6.1 (Sat Mar 22 02:14:22 2014) on "linux_x64"

testnest 项目的输出现在是:

nest-a: created
nest-a: enterView
nest-container: created
nest-container: enteredView
nest-b: created
nest-b: enteredView

如何围绕嵌套元素构造的顺序编写代码 - 之前之后包含元素 - 在 Dart 代码中仍然是一个开放的练习给读者:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多