克隆聚合物元素的正确方法是什么? [英] What is the correct way to clone a polymer element?

查看:50
本文介绍了克隆聚合物元素的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试克隆聚合物元素 - 通常是从某种数据创建的元素,因此仅使用构造函数或 document.createElement<创建模板的新实例不是一种选择/代码>.

  • element.cloneNode 不能单独工作,因为它不复制影子根
  • lodashPolymer Clone Objects 提出似乎没有任何作用(克隆对象为空)
  • cloneEl.shadowRoot.innerHTML = sourceEl.shadowRoot.innerHTML; 复制了 shadow-root,但似乎失去了绑定

广泛的例子:http://jsbin.com/vitumuwayu/3/edit

是否有我找不到的 Polymer.cloneNode 函数?

解决方案

我终于找到了这个问题的答案(至少对于 Polymer 1.0).

https://stackoverflow.com/a/6620327/1878199 展示了如何复制属性.https://stackoverflow.com/a/25187164/1878199 描述了如何获取聚合物元素的属性列表.

答案是:

newElement = element.cloneNode(true);for(var i in element.properties) {newElement[i] = 元素[i]}

完整说明和工作示例 在 jsbin 上 或在下面的代码段中.

<头><link rel="import" href="https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/polymer/polymer.html"><meta charset="utf-8"><title>聚合物克隆实施例</title><dom-module id="自定义元素"><模板><div style="border:solid"><input type="text" name="some_string" value="{{boundvalue::input}}"><p>{{boundvalue}}</p>

<脚本>聚合物({是:'自定义元素',特性: {边界值:{类型:字符串,value: '输入一些文字...',通知:真实}}});</dom-module><身体><custom-element id="source-element"></custom-element><p><button onclick="cloneElementWithoutProperties()">1 将上面的元素克隆到下面的列表中</button><p></p><button onclick="cloneElementWithProperties()">2 将上面的元素克隆到下面的列表中并复制属性</button><p></p><h3>测试描述</h3><ol><li>修改上面的文字</li><li>点击按钮 1</li><p></p><p>Observed:下面添加了一个元素,但它被重置为原始文本('输入一些文本...')</p><li>点击按钮 2</li><p></p><p>Observed:下面添加了一个元素,并保留了修改后的文本</p><p>同时验证功能是否还可以:修改副本中的输入,文本也更新了</p></ol><h3>克隆元素列表:</h3><div id='list-elements'>

</html><脚本>函数 cloneElementWithProperties() {list = document.querySelector("#list-elements");sourceEl = document.querySelector("#source-element")cloneEl = sourceEl.cloneNode(true);for (var i in sourceEl.properties) {cloneEl[i] = sourceEl[i];}list.insertBefore(cloneEl, null);}函数 cloneElementWithoutProperties() {list = document.querySelector("#list-elements");sourceEl = document.querySelector("#source-element")cloneEl = sourceEl.cloneNode(true);list.insertBefore(cloneEl, null);}</script>

I have been trying to clone polymer elements - typically elements that have been created from data of some kind, so it is not an option to just create a new instance of the template with the constructor or document.createElement.

  • element.cloneNode does not work on its own as it does not copy the shadow-root
  • lodash proposed by Polymer Clone Objects does not seem to do anything (cloned object is empty)
  • cloneEl.shadowRoot.innerHTML = sourceEl.shadowRoot.innerHTML; copies the shadow-root, but seems to lose the binding

Extensive example: http://jsbin.com/vitumuwayu/3/edit

Is there a Polymer.cloneNode function I have been unable to find?

解决方案

I have finally found an answer to this question (at least for Polymer 1.0).

https://stackoverflow.com/a/6620327/1878199 shows how to copy properties. https://stackoverflow.com/a/25187164/1878199 describes how to get the list of properties for a polymer element.

The answer is then:

newElement = element.cloneNode(true);
for(var i in element.properties) {
            newElement[i] = element[i]
          }

Full illustrated and working example on jsbin or in the snippet below.

<!DOCTYPE html>

<html>

<head>

  <link rel="import" href="https://cdn.rawgit.com/download/polymer-cdn/1.0.1/lib/polymer/polymer.html">



  <meta charset="utf-8">
  <title>Polymer Cloning Example</title>
</head>

<dom-module id="custom-element">
  <template>
    <div style="border: solid">
      <input type="text" name="some_string" value="{{boundvalue::input}}">
      <p>{{boundvalue}}</p>
    </div>

  </template>
  <script>
    Polymer({
      is: 'custom-element',
      properties: {
        boundvalue: {
          type: String,
          value: 'Enter some text...',
          notify: true
        }
      }
    });
  </script>
  </dom-module>



  <body>

    <custom-element id="source-element"></custom-element>
    <p>
      <button onclick="cloneElementWithoutProperties()">1 Clone the above element into the list below</button>
      <p></p>
      <button onclick="cloneElementWithProperties()">2 Clone the above element into the list below and also copy properties</button>
      <p></p>


      <h3>Test Description</h3>
      <ol>
        <li>Modify text above</li>
        <li>Click Button 1</li>
        <p></p>
        <p>Observed: An element is added below, but it is reset to the original text ('Enter some text...')</p>
        <li>Click Button 2</li>
        <p></p>
        <p>Observed: An element is added below, and it keeps the modified text</p>
        <p>Also verify that the functionality is still ok: Modify the input in the copy, the text is also updated</p>


      </ol>


      <h3>List of cloned elements:</h3>
      <div id='list-elements'>
      </div>

  </body>

</html>

<script>
  function cloneElementWithProperties() {

    list = document.querySelector("#list-elements");
    sourceEl = document.querySelector("#source-element")
    cloneEl = sourceEl.cloneNode(true);
    for (var i in sourceEl.properties) {
      cloneEl[i] = sourceEl[i];
    }
    list.insertBefore(cloneEl, null);

  }

  function cloneElementWithoutProperties() {

    list = document.querySelector("#list-elements");
    sourceEl = document.querySelector("#source-element")
    cloneEl = sourceEl.cloneNode(true);
    list.insertBefore(cloneEl, null);

  }
</script>

这篇关于克隆聚合物元素的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆