获取与核心项元素关联的模型的最佳方法是什么?

时间:2014-10-07 02:32:03

标签: dart dart-polymer

考虑这个聚合物片段:

<core-menu on-core-activate="{{selectUser}}">
  <template repeat="{{user in users}}">
    <core-item label="{{user.name}}"></core-item>
  </template>
</core-menu>

当用户点击core-item元素并触发selectUser时,如何确定哪些用户与已激活的core-item相关联?我想获取实际对象的实例,而不是我用来查找对象的字符串标识符。

1 个答案:

答案 0 :(得分:1)

聚合物&gt; = 1.0.0

@reflectable
void someClickHandler(dom.Event event, [_]) {
  // for native events (like on-click)
  var model = new DomRepeatModel.fromEvent(event);
  // or for custom events (like on-tap, works also for native events)
  var model = new DomRepeatModel.fromEvent(convertToJs(event));
  var value = model.jsElement['items']; 
  // or 
  var value = model.jsElement[$['mylist'].attributes['as']];
  // if you used the `as="somename"` 
  // in your <core-list> or <template is="dom-repeat">
}

有一个与自定义事件相关的未解决问题:https://github.com/dart-lang/polymer-dart/issues/624

聚合物&lt; = 0.16.0

在点击处理程序

import 'package:template_binding/template_binding.dart' as tb;

...

void onClick(MouseEvent e) {
  tb.TemplateInstance ti = tb.nodeBind(e.target).templateInstance; 
  var value = ti.model.value as Inner;
}

有关详细信息,请参阅In Polymer.js children of a template have a reference to the template, how can this be done in Polymer.dart