当数组 sub 属性 更改时计算绑定会触发吗?
Have computed binding fire when array sub property changes?
聚合物 1.*
我正在尝试改进我的计算绑定与数组的行为。我有 <div hidden$="[[getState(uploadState.*)]]">FOO</div>
但它经常发射。
我细化为uploadState.value
:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadState}}">
<div hidden$="[[getState(uploadState.value)]]">FOO</div>
有:
uploadState: {
type: Array,
value: function() {
var arr = Array.apply(null, Array(5));
var newArray = arr.map(()=> {
return {
value: false,
main: false,
edited: false,
loading: false
};
});
return newArray;
},
notify: true
},
attached: function() {
setTimeout(()=> this.set(`uploadState.0.value`, true), 1000)
}
但它根本没有开火。当 value
属性 改变时,如何让它在计算绑定中触发?
另外,我如何使用this.get()
来获取变化时的值?我在计算绑定 getState
中尝试了 var uploaded = this.get(['uploadState.value', 0])
,但它只显示未定义(当它曾经用 .* 触发时)
您使用绑定 uploadState.value
的问题是它不存在。您正在制作一个 uploadState
的数组,其中有一个 属性 value
的成员看起来更像 uploadState.*.value
但您真的不想更改所有更改值,只是有问题的那个,所以你可以利用 dom-repeat
的 item
绑定,这样你的代码就会像这样出现:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadState}}">
<div hidden$="[[item.value]]">FOO</div>
</template>
我可能建议您更改命名约定并使用 uploadStates
因为它是一个数组,所以您可以:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadStates}}"
as="uploadState">
<div hidden$="[[uploadState.value]]">FOO</div>
</template>
聚合物 1.*
我正在尝试改进我的计算绑定与数组的行为。我有 <div hidden$="[[getState(uploadState.*)]]">FOO</div>
但它经常发射。
我细化为uploadState.value
:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadState}}">
<div hidden$="[[getState(uploadState.value)]]">FOO</div>
有:
uploadState: {
type: Array,
value: function() {
var arr = Array.apply(null, Array(5));
var newArray = arr.map(()=> {
return {
value: false,
main: false,
edited: false,
loading: false
};
});
return newArray;
},
notify: true
},
attached: function() {
setTimeout(()=> this.set(`uploadState.0.value`, true), 1000)
}
但它根本没有开火。当 value
属性 改变时,如何让它在计算绑定中触发?
另外,我如何使用this.get()
来获取变化时的值?我在计算绑定 getState
中尝试了 var uploaded = this.get(['uploadState.value', 0])
,但它只显示未定义(当它曾经用 .* 触发时)
您使用绑定 uploadState.value
的问题是它不存在。您正在制作一个 uploadState
的数组,其中有一个 属性 value
的成员看起来更像 uploadState.*.value
但您真的不想更改所有更改值,只是有问题的那个,所以你可以利用 dom-repeat
的 item
绑定,这样你的代码就会像这样出现:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadState}}">
<div hidden$="[[item.value]]">FOO</div>
</template>
我可能建议您更改命名约定并使用 uploadStates
因为它是一个数组,所以您可以:
<template is="dom-repeat"
initial-count="1"
index-as="index"
items="{{uploadStates}}"
as="uploadState">
<div hidden$="[[uploadState.value]]">FOO</div>
</template>