【问题标题】:Polymer: observing property change for an array with data binding聚合物:观察具有数据绑定的数组的属性变化
【发布时间】:2015-10-14 17:59:48
【问题描述】:

my-custom-element 中,我试图根据第二个属性 (selected) 计算一个属性 (computedProperty)。第二个属性绑定到子元素 (iron-selector) 的属性 (selected-values)。由于观察到的属性是 Array 类型,因此我使用 expecting selected.* 语法来工作。它没有。

<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-selector/iron-selector.html">

<dom-module id="my-custom-element">

  <style>
    :host ::content .iron-selected {
      background-color: orange;
    }
  </style>
  <template>
    <div>logged:<span id='logged'></span></div>
    <div>computed:<span id='computed'>{{computedProperty}}</span></div>
    <iron-selector multi selected-values="{{selected}}" attr-for-selected="uid">
        <div class="select-option" uid="foo">foo</div>
        <div class="select-option" uid="bar">bar</div>
        <div class="select-option" uid="baz">baz</div>
    </iron-selector>
    <button on-tap="log">Log</button>
  </template>

  <script>
    Polymer({
      is: 'my-custom-element',
      properties: {
        selected: {
          type: Array,
          default: function () { return []; }
        },
        computedProperty: {
          type: String,
          computed: 'compute(selected.*)'
        }
      },
      compute: function(selected) {
        return this.selected.join();
      },
      log: function() {
        return this.$.logged.textContent = this.selected.join();
      }
    });
  </script>
</dom-module>

正如我可以使用日志按钮检查的那样,selected 属性的值已通过绑定正确传播。

我做错了什么?

【问题讨论】:

  • 我很想做一个 plunker 让人们可以玩弄它。但我没有看到链接到 github 上的 iron-selector 文件夹...

标签: data-binding polymer polymer-1.0 observers


【解决方案1】:

我创建了一个解决绑定问题的修复程序:

https://github.com/Trakkasure/iron-selector/tree/fix-multiselect

我已提出拉取请求以获取此修复程序。

【讨论】:

  • 感谢您的调查。我用"iron-selector": "Trakkasure/iron-selector#fix-multiselect" 更新了bower.json 中的依赖项并运行了bower update,但这并没有解决我的问题。我正在尝试弄清楚如何为这个问题编写单元测试。
  • 你能给我更多的细节吗?我能够获取您的代码并获得您期望的结果。 JSBin code
  • 我只是再次bower update,它现在完美运行!谢谢。 (更新将我的本地版本从提交 9f40405 移动到提交 6dba53131b 所以我不知道为什么我以前没有工作!)
猜你喜欢
  • 1970-01-01
  • 2016-10-25
  • 2018-03-17
  • 2015-01-18
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多