如何动态删除元素的单个属性的数据绑定?

时间:2014-12-12 13:52:04

标签: polymer

我需要在我的javascript中动态删除数据绑定

<my-element-a id="elem_a" label="{{ mylabel }}" value="{{ myvalue }}"></my-element>  
<my-element-b id="elem_b" label="{{ mylabel }}" value="{{ myvalue }}"></my-element>
<my-element-c id="elem_c" label="{{ mylabel }}" value="{{ myvalue }}"></my-element>

<my-element-a id="elem_a" label="{{ mylabel }}"></my-element>  
<my-element-b id="elem_b" label="{{ mylabel }}" value="{{ myvalue }}"></my-element>
<my-element-c id="elem_c" label="{{ mylabel }}" value="{{ myvalue }}"></my-element>

我尝试了什么

// 1: 
//Result: elem_b.value becomes null.
this.$.elem_a.removeAttribute('value');

// 2:
//Result: both elem_a.label and elem_a.value loses data binding
//Also inside polymer source I saw this was marked as something which would be
//deprecated
this.$.elem_a.unbindAll();

// 3:
//Result: Works but its dirty
//Create a copy of elem_a with its attributes and values
//except the value
var copy = copyAllExcept(elem_a, 'value');
var parent = this.$.elem_a.parentElement;
copy.removeAttribute('value');    
parent.removeChild(this.$.elem_a);
parent.appendChild(copy);

我理想的想法

//Something like this
this.$.elem_a.unbind('value');

请让我知道如何更好地解决这个问题?

注意:我使用聚合物版本0.3.1

1 个答案:

答案 0 :(得分:0)

Polymer正在使用名为TemplateBinding的库。这有一个公共方法来取消绑定模板中的属性。

https://www.polymer-project.org/docs/polymer/template.html#nodeunbind

我试图在聚合物模板中访问此功能,但我无法使其工作。通过一些调查,您可以找到答案。