2015-11-08 68 views
1

我有以下的情况: 这使得从一个阵列如何更新聚合物结合而不重新生成财产?

<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属性分配一个新的数组,但这看起来不正确。我觉得我错过了一些简单的步骤,但我无法弄清楚哪一个。有任何想法吗?

回答

0

参见

每个聚合物元件具有可用以下数组突变方法:

  • 推(路径,ITEM1,[.. 。,itemN])
  • 弹出(路径)
  • 不印字(路径,ITEM1,[...,itemN])
  • 移(路径)
  • 剪接(路径,索引,removeCount,[ITEM1,...,itemN] )

您需要使用这些高分子之一,收到通知的变化。

+1

是的,你是对的,我能够结合来自两个链接的信息,以得到它的权利。我使用'app.push(“cartItems”,item)'让项目出现在购物车中,并且为了改变数组内部对象的属性,我使用了与'path'相同的方法:该项目的索引,'var path =“cartItems”。 + itemIndex; app.set(path +“.quantity”,product.quantity); // [...]'并单独更新每个属性。 –