聚合物重置属性

时间:2015-10-16 14:10:17

标签: javascript polymer polymer-1.0

我在聚合物中使用[[]]绑定。它们是获取第一个Object的方式/函数吗?

我的问题是我更改了元素中的对象,然后我想将元素重置为内部更改之前的元素。 我想深入复制对象但是它会对对象上的聚合物函数产生问题。

<custom-elem item=[[item]]></custom-elem>

原着

item={a:123,b:234}

在自定义元素中,我将项目的值更改为

{a:241,b:382}

如何在custom-elem中获取原始项目?

感谢。

1 个答案:

答案 0 :(得分:1)

我能想到两个解决方案

  1. 分配如下的值

    <custom-elem item-orginal=[[item]] item=[[item]]></custom-elem>
    
  2. 在您的自定义元素中,当您想要重置项目时,请调用一个将重置该值的函数。

    resetItem: function() {
      this.item = this.itemOriginal
    }
    
    1. 在custom-elem中,只要您想重置值,就会触发自定义事件,如下所示。

      resetItem: function() {
        this.fire('custom-item-reset')
      }
      
    2. 在主持人中,侦听此事件并重置项目值。

      <custom-elem id="customElem" item=[[item]] on-custom-item-reset="resetCustomItem"></custom-elem>
      
      resetCustomItem: function() {
        this.$.customElem.item = this.item;
      }
      

      编辑:代码格式不清晰。所以做了一些修改。

相关问题