使用聚合物1.0在两个聚合物元件之间结合数据

时间:2015-07-22 18:19:50

标签: data-binding polymer web-component polymer-1.0 2-way-object-databinding

问题

  

在下面的示例中,如何将obj.name<input>字段的<test-element2>变量绑定到<test-element>

<小时/> 的背景

以下是我的代码。我有两种聚合物元素。 test-element的数据绑定为obj.nametest-element2有一个输入字段,由函数objChanged观察。无论我在输入字段中更改了什么值,它都会更改并打印在test-element2中,但更改不会反映在test-element中。任何机构都可以帮助将价值反映到test-element1吗?我有一个使用this.fire("object-change")的解决方案,当文本发生变化时,我正在寻找一个不使用事件监听器的解决方案。

还有一件事是我需要从脚本创建一个元素,它不能在HTML DOM中进行硬编码。

<小时/> 的代码:

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Demo</title>

    <script src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="../../bower_components/polymer/polymer.html"/>
</head>

<body>
    <dom-module id="test-element">
        <template>
            <div>Hello <span>{{obj.name}}</span></div>
        </template>

        <script>
            TestElement = Polymer({
                is: "test-element",

                properties: {
                    "obj": {
                        type: Object,
                        notify: true
                    }
                },

                observers: [
                    "objChanged(obj.name)"
                ],
                "objChanged": function() {
                    var that = this;
                    console.log("First element in 1",that.obj);
                }
            });
        </script>
    </dom-module>


    <dom-module id="test-element2">
        <template>
            <input value="{{obj.name::input}}"/>
        </template>

        <script>
            Polymer({
                is: "test-element2",

                properties: {
                    "obj": {
                        type: Object,
                        notify: true,
                        value: {
                            "name": "Charlie"
                        }
                    }
                },

                observers: [
                    "objChanged(obj.name)"
                ],

                ready: function() {
                    var element = new TestElement();
                    element.set("obj", this.obj);
                    this.appendChild(element);
                },

                "objChanged": function() {
                    console.log("changed in test-element2:", this.obj);
                }
            });
        </script>
    </dom-module>


    <test-element2></test-element2>
 </body>
</html>

5 个答案:

答案 0 :(得分:4)

如果您在<test-element> <template>中包含test-element2,则可以避免使用事件侦听器或观察者。这样test-element2为您处理input<test-element>之间的数据绑定。

下面是一个实时工作示例,它保留了obj属性,因为您已在元素中设置它。

<script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents.min.js"></script>
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="test-element">
  <template>
    <div>Hello <span>[[obj.name]]</span>
    </div>
  </template>
  <script>
    TestElement = Polymer({
      is: "test-element",

      properties: {
        "obj": {
          type: Object,
          notify: true
        }
      }
    });
  </script>
</dom-module>
<dom-module id="test-element2">
  <template>
    <input value="{{obj.name::input}}" />
    <test-element obj="[[obj]]"></test-element>
  </template>

  <script>
    Polymer({
      is: "test-element2",

      properties: {
        "obj": {
          type: Object,
          notify: true,
          value: {
            "name": "Charlie"
          }
        }
      }
    });
  </script>
</dom-module>
<test-element2></test-element2>

目前,<template is="dom-bind">以外的Polymer 1.0不支持命令式数据绑定

我建议您设置观察员,例如下面的示例或调整您的要求,以便<test-element>加入test-element2

button {
  display: block;
}
<script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents.min.js"></script>
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="test-element">
  <template>
    <div>Hello <span>[[obj.name]]</span>
    </div>
  </template>
  <script>
    TestElement = Polymer({
      is: "test-element",

      properties: {
        obj: {
          type: Object,
          notify: true
        }
      }
    });
  </script>
</dom-module>
<dom-module id="test-element2">
  <template>
    <input value="{{obj.name::input}}" />
  </template>

  <script>
    Polymer({
      is: "test-element2",

      properties: {
        obj: {
          type: Object,
          notify: true,
          value: {
            "name": "Charlie"
          }
        }
      },
      observers: ["objNameChanged(obj.name)"],
      objNameChanged: function(newValue) {
        Polymer.dom(document).querySelectorAll("test-element").forEach(function(element) {
          element.notifyPath("obj.name", newValue);
        });

        Polymer.dom(this.root).querySelectorAll("test-element").forEach(function(element) {
          element.notifyPath("obj.name", newValue);
        });
      }
    });
  </script>
</dom-module>
<test-element2></test-element2>
<button>Add test-element to <em>test-element2</em>
</button>
<button>Add test-element to <em>body</em>
</button>
<script>
  var testElement2 = document.querySelector("test-element2");

  var createTestElement = function(insertPoint) {
    var testElement = new TestElement();
    testElement.notifyPath("obj.name", testElement2.obj.name);

    insertPoint.appendChild(testElement);
  };

  document.querySelector("button:nth-of-type(2)").addEventListener("click", function() {
    createTestElement(Polymer.dom(document).querySelector("body"));
  });

  document.querySelector("button").addEventListener("click", function() {
    createTestElement(Polymer.dom(testElement2.root));
  });
</script>

答案 1 :(得分:3)

如果您选择将元素分解为自己的文件,则可以通过this Plunker examplenazerke)来演示双向数据绑定,方法是让一个组件观察另一个组件的属性。

代码

<强>的index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="parent-element.html">
  <link rel="import" href="first-child.html">
  <link rel="import" href="second-child.html"> </head>

<body>
  <parent-element></parent-element>
</body>

</html>

<强>父 - element.html

<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="parent-element">
  <template>
    <first-child prop={{value}}></first-child>
    <second-child feat1={{prop}}></second-child> In parent-element
    <h1>{{value}}</h1> </template>
  <script>
    Polymer({
      is: "parent-element",
      properties: {
        value: {
          type: String
        }
      },
      valueChanged: function() {
        console.log("value changed");
      }
    });
  </script>
</dom-module>

第一child.html

<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="first-child">
  <template>
    <p>first element.</p>
    <h2>{{prop}}</h2> </template>
  <script>
    Polymer({
      is: "first-child",
      properties: {
        prop: {
          type: String,
          notify: true
        }
      },
      ready: function() {
        this.prop = "property";
      }
    });
  </script>
</dom-module>

<强>第二child.html

<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html">
<dom-module id="second-child">
  <template>
    <p>Second element.</p>
    <h2>{{feat1}}</h2> </template>
  <script>
    Polymer({
      is: "second-child",
      properties: {
        feat1: {
          type: String,
          notify: true,
          value: "initial value"
        }
      },
      ready: function() {
        this.addEventListener("feat1-changed", this.myAct);
      },
      myAct: function() {
        console.log("feat1-changed ", this.feat1);
      }
    });
  </script>
</dom-module>

答案 2 :(得分:1)

如果您选择将元素分解为自己的文件,则可以使用<iron-meta>双向数据绑定as described here

示例代码:

<iron-meta key="info" value="foo/bar"></iron-meta>
...
meta.byKey('info').getAttribute('value').

document.createElement('iron-meta').byKey('info').getAttribute('value');

<template>
  ...
  <iron-meta id="meta"></iron-meta>
  ...
  this.$.meta.byKey('info').getAttribute('value');
  ....
</template>

答案 3 :(得分:1)

如果您选择将元素分解为自己的文件,则可以使用<iron-localstorage>双向数据绑定as described here

示例代码:

<dom-module id="ls-sample">
  <iron-localstorage name="my-app-storage"
    value="{{cartoon}}"
    on-iron-localstorage-load-empty="initializeDefaultCartoon"
  ></iron-localstorage>
</dom-module>

<script>
  Polymer({
    is: 'ls-sample',
    properties: {
      cartoon: {
        type: Object
      }
    },
    // initializes default if nothing has been stored
    initializeDefaultCartoon: function() {
      this.cartoon = {
        name: "Mickey",
        hasEars: true
      }
    },
    // use path set api to propagate changes to localstorage
    makeModifications: function() {
      this.set('cartoon.name', "Minions");
      this.set('cartoon.hasEars', false);
    }
  });
</script>

答案 4 :(得分:0)

JSONObject inputObj = j.getJsonFrmUrl(url);
 JSONArray air = inputObj.getJSONArray("results");
 for(int i=0;i<air.length();i++){ // your logic}