【发布时间】:2015-05-11 00:57:18
【问题描述】:
我正在编写一个聚合物应用程序。我扩展一个聚合物元素如下:
@CustomTag("custom-element")
class CustomElement extends PolymerElement {
@published List<String> years;
CustomElement.created() : super.created();
}
对应的html:
<link rel="import" href="../../../../packages/polymer/polymer.html">
<polymer-element name="custom-element" attributes="years">
<template>
Years listed are
<template repeat="{{ year in years }}">
<p> {{ year }} </p>
</template>
</template>
<script type="application/dart" src="custom_element.dart"></script>
</polymer-element>
在入口 html 文件中引用的 dart 脚本中,在我从服务器获取一些数据之后,我会动态创建这样的元素:
initPolymer().run((){
querySelect("#custom-elements").children.add(
new Element.tag("custom-element")
..id = "my_custom_element"
);
});
如果CustomElement 中的years 设置为某个值,给定代码工作正常,现在我想在我的主脚本中设置它。如果属性years 是String 类型,那么我只需将最后一块更改为
initPolymer().run((){
querySelect("#custom-elements").children.add(
new Element.tag("custom-element")
..id = "my_custom_element"
..setAttribute("years", "2010")
);
});
但问题是我需要设置整个List,而不是单个值。我想不出办法。我该怎么办?非常欢迎解决方案和建议。
编辑: 所以我的主脚本如下所示
void main(){
// in reality this list is fetched from the server
List<String> customYears = ['2010', '2011'];
initPolymer().run((){
querySelect("#custom-elements").children.add(
new Element.tag("custom-element")
..id = "my_custom_element"
// HERE I would like to set a public field *years*
// of the CustomElement
// the following does not work
// 1.
// ..setAttribute('years', customYears)
// Exception: Uncaught Error: type 'List' is not a subtype of type 'String' of 'value'.
// 2.
// ..setAttribute('years', toObservale(customYears))
// Exception: Uncaught Error: type 'ObservableList' is not a subtype of type 'String' of 'value'.
// 3.
// ..years = customYears
// ..years = toObservable( custom Years )
// NoSuchMethodError: method not found: 'years='
);
});
}
所以问题是,如何为类本身之外的 CustomElement 实例的 非字符串 成员字段赋值?课堂内的简单作业不会造成任何问题,但这不是我所寻求的。我希望我现在能更清楚地解释自己。
【问题讨论】:
-
对我来说不清楚实际问题是什么?你想在哪里设置列表?你能补充更多细节吗?
标签: dart dart-polymer