简体   繁体   English

数据绑定到聚合物中的数组

[英]Data Binding to Array in Polymer

I'm in the process of learning Polymer . 我正在学习Polymer I am trying to bind an array to my UI. 我正在尝试将数组绑定到我的UI。 Each object in the array has a property that will change. 数组中的每个对象都有一个将改变的属性。 I need my UI to update when the property value changes. 属性值更改时,我需要更新UI。 I've defined my Polymer component as follows: 我将聚合物成分定义如下:

my-component.html 我-component.html

<dom-module id="my-component">
    <template>
      <h1>Hello</h1>
      <h2>{{items.length}}</h2>
      <table>
        <thead>
          <tr>
            <th>Name</th>
            <th>Status</th>
          </tr>
        </thead>

        <tbody>
          <tr repeat="{{ item in items }}">
            <td>{{ item.name }}</td>
            <td>{{ item.status }}</td>
          </tr>
        </tbody>
      </table>

      <br />

      <button on-click="testClick">Test</button>
    </template>

    <script>
        // element registration
        Polymer({
            is: "my-component",
            properties: {
                items: {
                  type: Array,
                  value: function() {
                      return [
                        new Item({ name:'Tennis Balls', status:'Ordered' }),
                        new Item({ name:'T-Shirts', status: 'Ordered' })
                      ];
                  }  
                }                
            },

          testClick: function() {
            for (var i=0; i<items.length; i++) {
              if (items.name === 'Tennis Balls') {
                items[i].status = 'Shipped';
                break;
              }
            }                  
          }
        });
  </script> 
</dom-module>

The component renders. 组件渲染。 However, the bindings do not work at all. 但是,绑定根本不起作用。

  1. The line with {{ items.length }} does not show a count. 带有{{ items.length }}的行不显示计数。 Its basically just an empty h2 element. 它基本上只是一个空的h2元素。
  2. The first item gets rendered in the list. 第一项在列表中呈现。 However, the second one does not. 但是,第二个没有。
  3. When I click the Test button, the update to the status is not reflected on the screen. 当我单击“测试”按钮时,状态更新不会显示在屏幕上。

When I look at everything, it looks correct to me. 当我看一切时,对我来说看起来是正确的。 However, it is clear from the behavior that the data-binding is not setup properly. 但是,从行为可以明显看出,数据绑定设置不正确。 What am I doing wrong? 我究竟做错了什么? The fact that items.length and the initial rendering of all of the items in the array has me really confused. 我真的感到困惑,即items.length和数组中所有项目的初始呈现。

Polymer data binding system works like this: 聚合物数据绑定系统的工作方式如下:

If the declared property changes (for example adding a new item) then it will detect the change and update your DOM. 如果声明的属性发生更改(例如添加新项),它将检测到更改并更新您的DOM。

However Polymer won't monitor changes inside your property (For performance/compatibility reasons). 但是,Polymer不会监视物业内部的更改(出于性能/兼容性的原因)。

You need to notify Polymer that something inside your property changed. 您需要通知Polymer属性中的某些内容已更改。 You can do that using the set method or notifyPath . 您可以使用set方法或notifyPath做到这一点

Eg (From the polymer data binding section ) 例如(来自聚合物数据绑定部分

this.set('myArray.1.name', 'Rupert');

You can also add an observer if you want to do something when your array is updated. 如果要在更新数组时执行某些操作,也可以添加观察者。

Polymer 1.0 properties Documentation 聚合物1.0特性文档

And I think you should also add a notify:true to your property 而且我认为您还应该在您的媒体资源中添加一个notify:true

items: {
              type: Array,
              notify:true,
              value: function() {
                  return [
                    new Item({ name:'Tennis Balls', status:'Ordered' }),
                    new Item({ name:'T-Shirts', status: 'Ordered' })
                  ];
              }  
            }             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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