Polymer:观察具有数据绑定的数组的 属性 变化
Polymer: observing property change for an array with data binding
在 my-custom-element
中,我试图根据第二个 属性 (selected
) 计算 属性 (computedProperty
)。第二个 属性 绑定到子元素 (iron-selector
) 的 属性 (selected-values
)。由于 属性 观察到的是 Array 类型,因此我 expecting 可以使用 selected.*
语法。它没有。
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-selector/iron-selector.html">
<dom-module id="my-custom-element">
<style>
:host ::content .iron-selected {
background-color: orange;
}
</style>
<template>
<div>logged:<span id='logged'></span></div>
<div>computed:<span id='computed'>{{computedProperty}}</span></div>
<iron-selector multi selected-values="{{selected}}" attr-for-selected="uid">
<div class="select-option" uid="foo">foo</div>
<div class="select-option" uid="bar">bar</div>
<div class="select-option" uid="baz">baz</div>
</iron-selector>
<button on-tap="log">Log</button>
</template>
<script>
Polymer({
is: 'my-custom-element',
properties: {
selected: {
type: Array,
default: function () { return []; }
},
computedProperty: {
type: String,
computed: 'compute(selected.*)'
}
},
compute: function(selected) {
return this.selected.join();
},
log: function() {
return this.$.logged.textContent = this.selected.join();
}
});
</script>
</dom-module>
正如我可以使用日志按钮检查的那样,selected
属性 的值已通过绑定正确传播到。
我做错了什么?
我创建了一个解决绑定问题的修复程序:
https://github.com/Trakkasure/iron-selector/tree/fix-multiselect
我已发出拉取请求以获取此修复程序。
在 my-custom-element
中,我试图根据第二个 属性 (selected
) 计算 属性 (computedProperty
)。第二个 属性 绑定到子元素 (iron-selector
) 的 属性 (selected-values
)。由于 属性 观察到的是 Array 类型,因此我 expecting 可以使用 selected.*
语法。它没有。
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-selector/iron-selector.html">
<dom-module id="my-custom-element">
<style>
:host ::content .iron-selected {
background-color: orange;
}
</style>
<template>
<div>logged:<span id='logged'></span></div>
<div>computed:<span id='computed'>{{computedProperty}}</span></div>
<iron-selector multi selected-values="{{selected}}" attr-for-selected="uid">
<div class="select-option" uid="foo">foo</div>
<div class="select-option" uid="bar">bar</div>
<div class="select-option" uid="baz">baz</div>
</iron-selector>
<button on-tap="log">Log</button>
</template>
<script>
Polymer({
is: 'my-custom-element',
properties: {
selected: {
type: Array,
default: function () { return []; }
},
computedProperty: {
type: String,
computed: 'compute(selected.*)'
}
},
compute: function(selected) {
return this.selected.join();
},
log: function() {
return this.$.logged.textContent = this.selected.join();
}
});
</script>
</dom-module>
正如我可以使用日志按钮检查的那样,selected
属性 的值已通过绑定正确传播到。
我做错了什么?
我创建了一个解决绑定问题的修复程序:
https://github.com/Trakkasure/iron-selector/tree/fix-multiselect
我已发出拉取请求以获取此修复程序。