简体   繁体   English

在 Polymer 中使用 dom-if

[英]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) { ... ),因此当您返回truefalse您不是在isProductLiked函数中返回,而是在此匿名函数中返回。

Edit编辑

I am not familiar with Firebase so these may or may not work:我不熟悉 Firebase,所以这些可能有效也可能无效:

Possible solution #1可能的解决方案#1

  1. Query the product likes outside of the isProductLiked function.isProductLiked函数之外查询喜欢的产品。
  2. Set a property based on the result.根据结果​​设置属性。 (Lets call it productLikes for this example) (在这个例子中我们称之为productLikes
  3. Use this result in the dom-if like this: <template is="dom-if" if="{{isProductLiked(product, productLikes)}}">像这样在dom-if使用这个结果: <template is="dom-if" if="{{isProductLiked(product, productLikes)}}">
  4. Modify the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM