以编程方式创建Polymer元素

时间:2014-08-31 11:09:56

标签: dart dart-polymer

我有一个现有的应用程序(使用canvas标签的游戏​​),我想尝试使用PolymerDart为它创建UI元素(想想设置屏幕,与游戏中角色的对话交互)。

我正在尝试让基本点击计数器示例正常工作。我的index.html看起来像:

<html>
  <head>
    <title>PraxisClient</title>
    <script async type="application/dart" src="app.dart"></script>
    <script async src="packages/browser/dart.js"></script>
    <script src="packages/web_components/platform.js"></script>
    <script src="packages/web_components/dart_support.js"></script>
    <link rel="import" href="app/elements/click_counter.html">
  </head>
  <body>
    <div id="ui">
    </div>
  </body>
</html>

在我的应用程序中,我正在尝试使用:

创建元素
querySelector('#ui').children.add(new Element.tag('click-counter'));

点击计数器代码与https://www.dartlang.org/polymer/#data-binding

中给出的示例完全相同
<link rel="import" href="../packages/polymer/polymer.html">
<polymer-element name="click-counter">
  <template>
    <button on-click="{{increment}}">Click Me</button>
    <p>You clicked the button {{count}} times.</p>
  </template>
  <script type="application/dart" src="click_counter.dart"></script>
</polymer-element>

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

@CustomTag('click-counter')
class ClickCounterElement extends PolymerElement {
  @observable int count = 0;

  ClickCounterElement.created() : super.created();

  void increment(Event e, var detail, Node target) {
    count += 1;
  }
}

我得到的错误是:

必须通过主Dart脚本导入网址:click_counter.dart

然而,即使我将click_counter.dart导入我的应用程序它似乎没有帮助,我只是看到dom中的标记而没有执行任何代码。

如果我在元素(<polymer-element noscript>)中添加“noscript”属性,那么我会看到模板被显示(但没有执行代码)。

1 个答案:

答案 0 :(得分:1)

pubspec.yaml聚合物变压器配置中,每个入口页面都需要一个条目

transformers:
- polymer:
    entry_points:
    - web/index.html

pub serve应该在缺少此配置时创建警告。

<html>
  <head>
    <title>PraxisClient</title>
    <!-- <script src="packages/web_components/platform.js"></script>
         not necessary anymore with Polymer >= 0.14.0 -->
    <script src="packages/web_components/dart_support.js"></script>
    <link rel="import" href="app/elements/click_counter.html">
  </head>
  <body>
    <div id="ui">
    </div>
    <!-- moved down and removed the `async` attribute 
         these things get modified anyway when you run `pub build`
         if you want to have customized behavior you need to do it
         in a custom transformer or in the build output (as far as I know) -->
    <script type="application/dart" src="app.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

变化

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

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