[英]Using dom-if in Polymer
dom-if
is not working in the given code. dom-if
在给定的代码中不起作用。 The code is processing and in debugging it also returns true.代码正在处理,在调试中它也返回 true。 But the template is not displaying!
但是模板没有显示!
Why the template is not displaying?为什么模板不显示?
<template is="dom-repeat" items="[[persistedProducts]]" as="product">
<template is="dom-if" if="{{isProductLiked(product)}}">
<paper-button on-click="dislikeThisProduct"
title="Click to UnLike" class="title-rose">
<iron-icon icon="icons:thumb-up" class="title-rose" style="width:28px; height:17px;"></iron-icon>
Liked
</paper-button>
</template>
</template>
isProductLiked: function(product) {
let uid = this.user.uid;
//Add visitor/user details here..
this.$.queryProductLikes.disabled = false;
this.$.queryProductLikes.ref.child(product.$key).child(uid).on("value", function(snapshot) {
if(snapshot.val() != null) {
//Display Like - if Liked.
if(snapshot.val().isLiked) {
return true;
}
return false;
} else {
//Not Liked
return false;
}
});
}
I think the problem is in the isProductLiked
function:我认为问题出在
isProductLiked
函数中:
this.$.queryProductLikes.ref.child(product.$key).child(uid).on("value", function(snapshot) {
if (snapshot.val() != null) {
if (snapshot.val().isLiked) {
return true;
}
return false;
} else {
return false;
}
});
You use an anonymous function here ( ... function(snapshot) { ...
), so when you return true
or false
you aren't returning in the isProductLiked
function, but in this anonymous function.您在此处使用匿名函数(
... function(snapshot) { ...
),因此当您返回true
或false
您不是在isProductLiked
函数中返回,而是在此匿名函数中返回。
Edit编辑
I am not familiar with Firebase so these may or may not work:我不熟悉 Firebase,所以这些可能有效也可能无效:
Possible solution #1可能的解决方案#1
isProductLiked
function.isProductLiked
函数之外查询喜欢的产品。productLikes
for this example) productLikes
)dom-if
like this: <template is="dom-if" if="{{isProductLiked(product, productLikes)}}">
dom-if
使用这个结果: <template is="dom-if" if="{{isProductLiked(product, productLikes)}}">
isProductLiked
function to include the productLikes
parameter and to handle the new logic.isProductLiked
函数以包含productLikes
参数并处理新逻辑。 The idea here is, to remove any async function call from the isProductLiked
function.这里的想法是,从
isProductLiked
函数中删除任何异步函数调用。
Possible solution #2可能的解决方案#2
Redesign the data storage and query, so the persistedProducts
contains the likes too.重新设计数据存储和查询,因此
persistedProducts
包含likes。 So you could write the dom-if
like this: <template is="dom-if" if="{{product.isLiked}}">
.所以你可以这样写
dom-if
: <template is="dom-if" if="{{product.isLiked}}">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.