【问题标题】:Styling Polymer Element Dynamically动态造型聚合物元素
【发布时间】:2015-08-25 03:27:06
【问题描述】:

我正在尝试使纸卡元素根据 Fire base 上客户数据的状态更改颜色,但由于某种原因,颜色仅在客户第二次点击时更新。现在,我将纸卡 ID 设置为 firebase 数据,以使其更改颜色。这是我的元素样式代码:

<style is="custom-style">
:host {
  display: block;
}
#cards {
  @apply(--layout-vertical);
  @apply(--center-justified);
}
.row {
  padding: 20px;
  margin-left: 10px;
}
paper-card {
  padding: 20px;
}
#check {
  float: right;
  bottom: 15px;
  --paper-card
}
#Done {
  --paper-card-header: {
    background: var(--paper-green-500);
  };

  --paper-card-content: {
    background: var(--paper-green-300);
  };
}
#Default {
  /*Apply Default Style*/
  /*--paper-card-content: {*/
  /*  background: var(--paper-red-500);*/
  /*};*/
}
paper-icon-button.check{
  color: var(--paper-green-500);
}
paper-icon-button.check:hover{
  background: var(--paper-green-50);
  border-radius: 50%;
}
#check::shadow #ripple {
  color: green;
  opacity: 100%;
}
.iron-selected{
  color: green;
}

这是模板:

  <template>

<firebase-collection
  location="https://calllistmanager.firebaseio.com/Wilson"
  data="{{wilsonData}}"></firebase-collection>

  <div id="cards">
    <template id="cards" is="dom-repeat" items="{{wilsonData}}" as="customer">
      <paper-card id="{{customer.status}}" class="{{customer.status}}" heading="[[customer.__firebaseKey__]]">
        <div class="card-content">
            <span>Phone: </span><span>[[customer.number]]</span>
            <span>Status: </span><span>[[customer.status]]</span>
            <paper-icon-button style="color: green" id="check" on-tap="checktap" icon="check">
            </paper-icon-button>
          </div>
      </paper-card>
  </template>
  </div>

这是我的脚本:

<script>

(函数() { 聚合物({ 是:'列表显示',

  properties: {
    wilsonData: {
      type: Object,
      observer: '_dataObserver'
    }
  },

  ready: function() {
      var listRef = new Firebase("https://calllistmanager.firebaseio.com/Wilson");
    },

  checktap: function(e){
    // e.model.customer.status = "Done";
    console.log("Starting Status: " + e.model.customer.status);
    ref = new Firebase("https://calllistmanager.firebaseio.com/Wilson")
    var stat;
    var store = ref.child(e.model.customer.__firebaseKey__);
    store.on("value", function(snapshot){
      stat = snapshot.child("status").val();
    });
    if(stat == "Done"){
      store.update({
        "status": "Default"
      });
      e.model.customer.status = "Default";
    }
    else {
        store.update({
        "status": "Done"
      });
      e.model.customer.status = "Done";
    }
    console.log("Ending Status: " + e.model.customer.status);
    this.updateStyles()
  }
});

})();

起初我认为问题可能是函数运行 updateStyles();比 firebase 更新速度更快,但在第二次点击时它总是能正常工作……有什么建议吗?

【问题讨论】:

    标签: javascript css polymer


    【解决方案1】:

    我认为问题可能是由调用 firebase 引起的。 store.on("value", 不是同步函数。但是,稍后在您的代码中,您假设您已经有一个值,该值将在稍后触发 value 事件时设置。您可以尝试在事件处理程序中添加其余代码。像这样:

    checktap: function(e){
        // e.model.customer.status = "Done";
        console.log("Starting Status: " + e.model.customer.status);
        ref = new Firebase("https://calllistmanager.firebaseio.com/Wilson")
        var store = ref.child(e.model.customer.__firebaseKey__);
        store.once("value", function(snapshot){
            var stat = snapshot.child("status").val();
            if(stat == "Done"){
              store.update({
                "status": "Default"
              });
              e.model.set("customer.status", "Default");
            }
            else {
                store.update({
                "status": "Done"
              });
              e.model.set("customer.status", "Done");
            }
            console.log("Ending Status: " + e.model.customer.status);
            this.updateStyles();
        }.bind(this));
    }
    

    基本上,您要等到stat 变量设置完成后才能完成其余任务。另请注意,末尾的 bind(this) 将允许您从事件处理程序更新样式。

    更新

    还有几个问题。首先,最好使用类来更改样式而不是 ID。 ID 不应更改。然后,要绑定到类属性,请使用$ 符号。更新模型时,应使用set API。 看看this plunker。这是一个小的工作示例(仅适用于 Chrome),当您单击复选标记时会更改样式。但是,它不使用 Firebase。

    以下是您可以如何使用类来设置样式。

    .Done {
      --paper-card-header: {
        background: var(--paper-green-500);
      };
    
      --paper-card-content: {
        background: var(--paper-green-300);
      };
    }
    

    在你的模板中:

    <paper-card class$="{{customer.status}}" heading="[[customer.__firebaseKey__]]">
    

    【讨论】:

    • 我将 .bind(this) 添加到我的函数末尾,然后我收到了 updatestyles 错误。现在似乎正在调用没有大写 S 的函数......奇怪
    • 您是否也将其余代码移到了事件处理程序中?
    • 另外,我刚刚注意到我的代码中有一个错字,我刚刚更正了。
    • 其他代码是什么意思?这一切都在事件处理函数中
    • 我指的是 Firebase 的 on-value 事件处理程序。本质上,我只是想问您是否尝试复制粘贴上面的整个解决方案,或者只是附加了bind。由于我无法运行您的代码,因此无法验证它是否真的有效。
    猜你喜欢
    • 2014-08-24
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多