Data binding 聚合物if模板结合性能
元素有很多功能,允许像Data binding 聚合物if模板结合性能,data-binding,polymer,polymer-1.0,Data Binding,Polymer,Polymer 1.0,元素有很多功能,允许像这样的很棒的表达式在数组为空时轻松显示一些消息 现在,我想知道最好的办法是什么 我提出了两种方案,但我不确定哪种方案对性能最好 第一个选项是有一个额外的变量,该变量是只读的,并且每当数组更改时都会计算 <template is="dom-if" if="{{itemsEmpty}}"> The array is empty! </template> <script> Polymer({ is: 'my-ele
这样的很棒的表达式在数组为空时轻松显示一些消息
现在,我想知道最好的办法是什么
我提出了两种方案,但我不确定哪种方案对性能最好
第一个选项是有一个额外的变量,该变量是只读的
,并且每当数组更改时都会计算
<template is="dom-if" if="{{itemsEmpty}}">
The array is empty!
</template>
<script>
Polymer({
is: 'my-element',
properties: {
items: {
type: Array
},
itemsEmpty: {
type: Boolean,
computed: 'computeItemsEmpty(items)',
readOnly: true
}
},
ready: function() {
this.items = [{'name': 'Jack'}, {'name': 'Skellington'}];
},
computeItemsEmpty: function(items){
return items.length == 0;
}
});
</script>
那么,以下哪一种是最佳实践,或者是否有其他方法可以做到这一点?我还尝试在if
中使用函数,但当然没有重新计算。在Polymer 1.0中,仍然可以在表达式中使用对象属性。数组有一个长度属性,0很容易是假的,所以我只想这样做:
在Polymer 1.0中,您仍然可以在表达式中使用对象属性。数组有一个length属性,0很容易出错,所以我只想这样做:
我相信也可以在其中运行函数。这在观察多个属性时非常有用
<template is="dom-if" if="[[arrayEmpty(items, itemsTwo)]]">
The array is empty!
</template>
<script>
Polymer({
is: 'my-element',
properties: {
items: {
type: Array,
},
itemsTwo: {
type: Array,
}
},
arrayEmpty: function(item ,itemsTwo) {
if(item && itemsTwo){
return true;
}
}
});
数组是空的!
聚合物({
是‘我的元素’,
特性:{
项目:{
类型:数组,
},
项目二:{
类型:数组,
}
},
arrayEmpty:函数(项,项two){
如果(项目和项目两){
返回true;
}
}
});
我相信也可以在其中运行函数。这在观察多个属性时非常有用
<template is="dom-if" if="[[arrayEmpty(items, itemsTwo)]]">
The array is empty!
</template>
<script>
Polymer({
is: 'my-element',
properties: {
items: {
type: Array,
},
itemsTwo: {
type: Array,
}
},
arrayEmpty: function(item ,itemsTwo) {
if(item && itemsTwo){
return true;
}
}
});
数组是空的!
聚合物({
是‘我的元素’,
特性:{
项目:{
类型:数组,
},
项目二:{
类型:数组,
}
},
arrayEmpty:函数(项,项two){
如果(项目和项目两){
返回true;
}
}
});
两种解决方案在性能上应该是等效的,因为它们基本上使用相同的基础方法(setter
)。两种解决方案在性能上应该是等效的,因为它们基本上使用相同的基础方法(setter
)。