在另一个自定义标签内动态创建聚合物元素
Dynamically creating polymer element inside another custom tag
需要在另一个聚合物元素的脚本中动态创建聚合物元素办公室列表,如下所示:
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response = "handleResponse"></iron-ajax>
</template>
<script>
Polymer({
is: "contacts-tag",
handleResponse: function(request){
var response = request.detail.response;
this.officesRegions = response.officesRegions;
this.officesCities = response.officesCities;
var dynamicEl = document.createElement("offices-list");
dynamicEl.setAttribute("regions", this.officesRegions);
dynamicEl.setAttribute("cities", this.officesCities);
document.body.appendChild(dynamicEl);
}
});
</script></dom-module>
但是一旦到达 "document.createElement("offices-list");"这个新标签内的元素开始呈现,并且它的就绪方法已经被调用,而我期望它们在我设置属性后发生。我该怎么做?
编辑: 看来问题的性质不同。我将对象设置为属性,但 "offices-list" 标记无法识别它们,因此无法访问它或循环遍历它。然后,我的问题会变成,"How to bind objects?"
你不应该使用setAttribute
,你应该直接设置相应的属性。
var dynamicEl = document.createElement("offices-list");
dynamicEl.regions = this.officesRegions;
dynamicEl.cities = this.officesCities;
document.body.appendChild(dynamicEl);
我认为正确的答案是 - 这取决于您要实现的目标。
如果您希望强制性地数据绑定,即您想将类似的内容动态添加到模板中,
<offices-list offices-regions="{{regions}}"
offices-cities="{{cities}}">
</offices-list>
你 因为 Polymer 1.0 不支持命令式数据绑定。
如果您只是想将参数(NOT 数据绑定)传递到动态创建的元素中,el.setAttribute()
和 el.myAttribute = this.myAttribute
都应该有效.
由于您使用的是 Polymer,我认为您应该尝试在框架内(在 Polymer 模板实例内)包含 DOM 操作,而不是直接添加到 document.body
,像这样。
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response="handleResponse"></iron-ajax>
<div id="insertion_point"></div>
</template>
<script>
Polymer({
is: "contacts-tag",
handleResponse: function(request) {
...
Polymer.dom(this.$.insertion_point).appendChild(dynamicEl);
}
});
</script>
</dom-module>
最后,如果您的目的是在 ajax 请求完成后简单地显示一个 <contacts-tag>
,我认为您可以通过声明方式实现。
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response="handleResponse"></iron-ajax>
<template is="dom-if" if="{{_unveilOfficesListWhenReady}}">
<offices-list offices-regions="{{_regions}}"
offices-cities="{{_cities}}">
</offices-list>
</template>
</template>
<script>
Polymer({
is: "contacts-tag",
properties: {
_regions: String,
_cities: String,
_unveilOfficesListWhenReady: { type: Boolean, value: false }
},
handleResponse: function(request) {
var response = request.detail.response;
this._regions = response.officesRegions;
this._cities = response.officesCities;
this._unveilOfficesListWhenReady = true;
}
});
</script>
</dom-module>
不需要动态元素。
您可以使用聚合物团队提供的dom api。
Polymer.dom(parent).appendChild(polymerChild)
需要在另一个聚合物元素的脚本中动态创建聚合物元素办公室列表,如下所示:
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response = "handleResponse"></iron-ajax>
</template>
<script>
Polymer({
is: "contacts-tag",
handleResponse: function(request){
var response = request.detail.response;
this.officesRegions = response.officesRegions;
this.officesCities = response.officesCities;
var dynamicEl = document.createElement("offices-list");
dynamicEl.setAttribute("regions", this.officesRegions);
dynamicEl.setAttribute("cities", this.officesCities);
document.body.appendChild(dynamicEl);
}
});
</script></dom-module>
但是一旦到达 "document.createElement("offices-list");"这个新标签内的元素开始呈现,并且它的就绪方法已经被调用,而我期望它们在我设置属性后发生。我该怎么做?
编辑: 看来问题的性质不同。我将对象设置为属性,但 "offices-list" 标记无法识别它们,因此无法访问它或循环遍历它。然后,我的问题会变成,"How to bind objects?"
你不应该使用setAttribute
,你应该直接设置相应的属性。
var dynamicEl = document.createElement("offices-list");
dynamicEl.regions = this.officesRegions;
dynamicEl.cities = this.officesCities;
document.body.appendChild(dynamicEl);
我认为正确的答案是 - 这取决于您要实现的目标。
如果您希望强制性地数据绑定,即您想将类似的内容动态添加到模板中,
<offices-list offices-regions="{{regions}}"
offices-cities="{{cities}}">
</offices-list>
你
如果您只是想将参数(NOT 数据绑定)传递到动态创建的元素中,el.setAttribute()
和 el.myAttribute = this.myAttribute
都应该有效.
由于您使用的是 Polymer,我认为您应该尝试在框架内(在 Polymer 模板实例内)包含 DOM 操作,而不是直接添加到 document.body
,像这样。
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response="handleResponse"></iron-ajax>
<div id="insertion_point"></div>
</template>
<script>
Polymer({
is: "contacts-tag",
handleResponse: function(request) {
...
Polymer.dom(this.$.insertion_point).appendChild(dynamicEl);
}
});
</script>
</dom-module>
最后,如果您的目的是在 ajax 请求完成后简单地显示一个 <contacts-tag>
,我认为您可以通过声明方式实现。
<dom-module id="contacts-tag">
<template>
<iron-ajax ... on-response="handleResponse"></iron-ajax>
<template is="dom-if" if="{{_unveilOfficesListWhenReady}}">
<offices-list offices-regions="{{_regions}}"
offices-cities="{{_cities}}">
</offices-list>
</template>
</template>
<script>
Polymer({
is: "contacts-tag",
properties: {
_regions: String,
_cities: String,
_unveilOfficesListWhenReady: { type: Boolean, value: false }
},
handleResponse: function(request) {
var response = request.detail.response;
this._regions = response.officesRegions;
this._cities = response.officesCities;
this._unveilOfficesListWhenReady = true;
}
});
</script>
</dom-module>
不需要动态元素。
您可以使用聚合物团队提供的dom api。
Polymer.dom(parent).appendChild(polymerChild)