如何在不重新实例化 属性 的情况下更新聚合物绑定?

How can I update polymer binding without reinstantiating property?

我遇到以下情况: 从数组中呈现项目的自定义元素

<dom-module id="shopping-cart">
 <template>
  <style>
    :host {
      display: block;
    }
  </style>
  <template is="dom-repeat" items=[[cartItems]]>
    <tr>
      <td>[[item.name]]</td>
      <td><span>[[item.quantity]]</span> <small>[[item.uom]]</small></td>
      <td><span>[[item.cost]]</span> <small>lei</small></td>
    </tr>
  </template>
 </template>
<script>
  (function() {
  'use strict';

  Polymer({
    is: 'shopping-cart',

    properties: {
      cartItems: {
        type: Object,
        value: []
      }
    }
  });
})();
</script>
<!-- And this is the container -->
<template is="dom-bind" id="app">
  <shopping-cart cart-items="[[cartItems]]"></shopping-cart>
</template>

我正在根据一些事件操纵 app.cartItems 对象。我希望绑定在更改数组时更新,但事实并非如此。推送新元素或删除其他元素时,更改不会反映在 shopping-cart 元素中。我试图手动触发 cart-items-changed 事件,但这仍然无济于事。唯一似乎导致绑定刷新的是为 app.cartItems 属性 分配一个新数组,但这似乎不正确。我觉得我错过了一些简单的步骤,但我不知道是哪一步。有什么想法吗?

Every Polymer element has the following array mutation methods available:

  • push(path, item1, [..., itemN])
  • pop(path)
  • unshift(path, item1, [..., itemN])
  • shift(path)
  • splice(path, index, removeCount, [item1, ..., itemN])

您需要为 Polymer 使用其中之一才能收到有关更改的通知。