Javascript 以编程方式设置聚合中数组项的属性值

Javascript 以编程方式设置聚合中数组项的属性值,javascript,polymer,Javascript,Polymer,我有一个应用程序,使用聚合物。在这个应用程序中,我有一个如下所示的组件: my component.html <dom-module id="view-tests"> <template> <table> <tbody> <template is="dom-repeat" items="{{ items }}" as="item">

我有一个应用程序,使用聚合物。在这个应用程序中,我有一个如下所示的组件:

my component.html

<dom-module id="view-tests">
    <template>
      <table>
        <tbody>
            <template is="dom-repeat" items="{{ items }}" as="item">              
              <tr>
                <td>[[ item.name ]]</td>
                <td><item-status status="[[ item.status ]]"></item-status></td>
              </tr>
            </template>
        </tbody>
      </table>

      <button on-click="bindClick">Bind</button>
    </template>

    <script>
        Polymer({
          is: "my-component",
          properties: {
            items: {
              type: Array,
              notify: true,
              value: function() {
                return [
                  new Item({ name:'Item 1', status:'In Stock' }),
                  new Item({ name:'Item 2', status:'Sold Out' })
                ];
              }  
            },
          },

          bindClick: function() {
            var items = items;              
            for (var i=0; i<items.length; i++) {
              if (i === 1) {
                items[i].status = 'In Stock';
              }
            }
          }          
        });
    </script>
</dom-module>
<dom-module id="test-status">
    <template>
        <span class$="{{ statusClass }}">{{ status }}</span>
    </template>

    <script>
        Polymer({
            is: "item-status",
            properties: {
                status: {
                    type: String,
                    value: '',
                    observer: '_statusChanged'
                }               
            },

            _statusChanged: function(newValue, oldValue) {
                if (newValue === 'In Stock') {
                  this.statusClass = 'green';
                } else if (newValue === 'Sold Out') {
                  this.statusClass = 'red';
                } else {
                  this.statusClass = 'black';
                }
            }
        });
  </script> 
</dom-module>
初始数据绑定工作正常。但是,当我单击“绑定”按钮时,文本不会像我预期的那样更新。此外,文本颜色没有像我预期的那样改变。我有
var items=items故意,因为在我的真实代码中,发生了回调,我必须将项传递到回调中。我不确定是否有更好的办法。不过,我的观点没有得到正确更新

谢谢你能提供的任何帮助

  • 当我单击“绑定”按钮时,文本不会像我预期的那样更新
  • 要使其工作,必须使用此.set('item.1.status','In Stock')。有关数组绑定的更多详细信息,请阅读

  • 此外,文本颜色没有像我预期的那样改变
  • 你只是在设置课程。你必须设计这个项目。在项目状态中包括样式标记,如下所示

    <style>
          .red {
            color: red;
          }
          .green {
            color: green;
          }
    </style> 
    
    
    瑞德先生{
    颜色:红色;
    }
    格林先生{
    颜色:绿色;
    }
    
    3.我认为var项目=项目;故意的,因为在我的真实代码中,发生了回调

    我认为您无法在回调中设置数组项的值并使其正常工作。如果你发布更多关于你的场景的细节,有人可能会帮助你

  • 最好的做法是让dom模块id和聚合物“is”id相同。这是我第一次遇到不同的ID,我几乎可以肯定它会破坏一些东西
  • 工作jsbin: