Understanding two-way databinding in Polymer

孤人 提交于 2019-12-12 04:42:19

问题


I'm new to polymer, and I've been reading the docs and playing around with it a little bit, but I'm having some difficulty understanding the difference between the [[]] one way data flow, and the {{}} two way data flow.

Can anyone help me understand the difference?


回答1:


For example handing data from one element down to one of it's child elements is considered one-way-data-binding. If this data get's modified in the child element the change is also reflected in the parent. This is made possible through a two-way-data-binding as it can go in both directions up and down.

Below I have added two code examples.
The first illustrates one-away-data-binding between two elements. The second example shows two-way-data-binding where the parent element reflects a change that happened in it's child.

Please note that I am using the syntax of Polymer 1. However, I believe the way data-binding is handled in Polymer 2 hasn't changed.


One-way-data-binding
Parent Element

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../child-element/child-element.html">
<dom-module id="parent-element">
  <template>
    <style>
    </style>
    <div>
      <h1>[[name]]</h1> <!-- This will print Pazzle -->

      <!-- Use the imported child element and bind the name property-->
      <child-element name="[[name]]"></child-element>
      <!-- Will print Pazzle in a h2 element -->

    </div>
  </template>
  <script>
    Polymer({
      is: 'parent-element',
      properties: {
        name: {
          type: String,
          value: 'Pazzle'
        }
      },
    });
  </script>
</dom-module>

Child of parent-element:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="child-element">
  <template>
    <style>
    </style>
    <div>
      <h2>[[name]]</h2> <!-- This will print Pazzle -->
    </div>
  </template>
  <script>
    Polymer({
      is: 'child-element',
      properties: {
        name: {
          type: String,
          value: ''
        }

      },
    });
  </script>
</dom-module>



Two-way-data-binding
Parent Element

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../child-element/child-element.html">
<dom-module id="parent-element">
  <template>
    <style>
    </style>
    <div>
      <!-- Use the imported child element and bind the name property-->
      <child-element name="{{name}}"></child-element>
      <!-- Will print Contis in a h2 element instead of Pazzle -->

    </div>
  </template>
  <script>
    Polymer({
      is: 'parent-element',
      properties: {
        name: {
          type: String,
          value: ''
        }
      },

      ready: function() {
        this.name = "Pazzle";
      }

    });
  </script>
</dom-module>

Child of parent-element:

<link rel="import" href="../../bower_components/polymer/polymer.html">
<dom-module id="child-element">
  <template>
    <style>
    </style>
    <div>
      <h2>[[name]]</h2> <!-- This will print Contis -->
    </div>
  </template>
  <script>
    Polymer({
      is: 'child-element',
      properties: {
        name: {
          type: String,
          value: '',
          notify: true,
          // notify will make it possible to send
          // our changed Name property back up
          observer: 'nameChanged'
        }
      },

      nameChanged: function() {
        if(this.name == 'Pazzle' || this.name == ''){
          this.name = "Contis";
        }
      }

    });
  </script>
</dom-module>


来源:https://stackoverflow.com/questions/46594492/understanding-two-way-databinding-in-polymer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!