在 Polymer.js 模板的子级中有对模板的引用,这如何在 Polymer.dart 中完成 [英] In Polymer.js children of a template have a reference to the template, how can this be done in Polymer.dart
本文介绍了在 Polymer.js 模板的子级中有对模板的引用,这如何在 Polymer.dart 中完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我引用由 生成的元素时,在 Polymer.js 中,此类元素具有一个属性
templateInstance
,该属性提供对它的模板就像这里使用的一样:
https://github.com/PolymerLabs/polymer-selector/blob/master/polymer-selector.html#L286
解决方案
Polymer >= 1.0.0
@reflectablevoid someClickHandler(dom.Event 事件, [_]) {//对于原生事件(如点击)var model = new DomRepeatModel.fromEvent(event);//或自定义事件(如 on-tap,也适用于本机事件)var model = new DomRepeatModel.fromEvent(convertToJs(event));var value = model.jsElement['items'];//或者var value = model.jsElement[$['mylist'].attributes['as']];//如果你使用了 `as="somename"`//在你的 <core-list>或 <template is="dom-repeat">}
有一个与自定义事件相关的未决问题:https://github.com/dart-lang/polymer-dart/issues/624
聚合物 <= 0.16.0
编辑
下面的例子只需要这3行,其他代码只是为了演示
import 'package:template_binding/template_binding.dart' as tb;tb.TemplateInstance ti = tb.nodeBind(e.target).templateInstance;var value = ti.model.value 作为内部;
编辑结束
最近添加了此功能(请参阅 https://code.google.com/p/dart/issues/detail?id=17462)
我创建了一个示例来测试它是如何工作的:
index.html
<头><title>嵌套重复</title><!-- <script src="packages/web_components/platform.js"></script>不再需要 Polymer >= 0.14.0 --><script src="packages/web_components/dart_support.js"></script><link rel="import" href="nested_templates.html">头部><身体><嵌套模板></嵌套模板><script type="application/dart">export 'package:polymer/init.dart';</script>