如何将任意数据从Dart聚合物Web组件传递到单击事件处理函数

时间:2013-08-15 08:39:08

标签: dart dart-webui polymer

在Dart的Web UI中,可以传递任意数据以响应事件,例如,以下代码段将值2传递给increment(int incBy)方法以响应按钮的开启 - 点击事件:

<!-- Web UI -->
<element name="x-click-counter">
  <template>
    <button on-click="increment(2)">  <!-- passing a value of 2 -->
      Click me
    </button>   
  </template>
</element>
<script>
  import 'package:web_ui/web_ui.dart';

  class CounterComponent extends WebComponent {
    void increment(int incBy) {        // accept the value of 2
      count = count + incBy;
    }
  }
</script>

在Polymer(和Polymer.dart)中,on-click事件属性需要函数名的字符串版本,而不是实际的函数调用。这在polymer docs page上描述为:

  

事件处理程序属性的值是方法的字符串名称   在组件上。与传统语法不同,您不能放置可执行文件   属性中的代码。

使用polymer.dart,如下所示:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="increment">  <!-- can't pass a value of 2, as you need to pass a string -->
      Click Me
    </button>
  </template>
</polymer-element>
<script>
  import 'package:polymer/polymer.dart';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement with ObservableMixin {
    @observable int count = 0;

    void increment(Event event, var detail, var target) {  // How do I pass 2 to this function?
      count = count ++;
    }
  }
</script>

问题:如何将任意值传递给increment函数?

3 个答案:

答案 0 :(得分:19)

您可以使用html data-属性传递额外数据,然后通过target参数访问它们。

重新编写聚合物示例以添加data-incby字段,该字段的值增加计数,如下所示:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="increment"  data-incby="2">  <!-- now passing the value as a data attribute -->
      Click Me
    </button>
  </template>
</polymer-element>
<script>
  import 'package:polymer/polymer.dart';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement with ObservableMixin {
    @observable int count = 0;

    void increment(Event event, var detail, var target) {
      int incBy = int.parse(target.attributes['data-incby']);  // extract the value 2
      count = count + incBy;
    }
  }
</script>

答案 1 :(得分:14)

自从Chris回答以来,Dart和Polymer.dart发生了变化。这是Dart v1.0的更新代码:

<polymer-element name="x-click-counter">
  <template>
    <button on-click="{{increment}}"  data-incby="2">  <!-- now passing the value as a data attribute -->
      Click Me
    </button>
    <span>{{count}}</span>
  </template>
</polymer-element>
<script type="application/dart">
  import 'package:polymer/polymer.dart';
  import 'dart:html';

  @CustomTag("x-click-counter")
  class CounterComponent extends PolymerElement {
    @observable int count = 0;

    CounterComponent.created() : super.created();

    void increment(Event event, var detail, var target) {
      int incBy = int.parse(target.attributes['data-incby']);  // extract the value 2
      count = count + incBy;
    }
  }
</script>

答案 2 :(得分:1)

我对Polymer 0.11.0 + 5的解决方案

element.html

<link rel="import" href="../packages/polymer/polymer.html">
<polymer-element name="dp-element">
  <template>
    <div class="row">
    <ul>
      <template repeat="{{ item in items }}">
        <li on-click="{{load}}" data-incby="{{item}}">{{ item }}</li>
      </template>
    </ul>

  </template>

 <script type="application/dart">
  import 'package:polymer/polymer.dart';
  import 'view.dart';
  import 'dart:html';

  @CustomTag('dp-element')
  class DpElement extends PolymerElement {

  @observable List<String> items;

  DpElement.created() : super.created(){
  }
  void load(Event event, var detail, var target) {
     String incBy = target.attributes['data-incby'];
     print(incBy);
  }
}