【问题标题】:Polymer 1.0: Two-way data binding: <iron-form> to/from FirebasePolymer 1.0:双向数据绑定:<iron-form> 与 Firebase 之间
【发布时间】:2015-11-08 20:15:08
【问题描述】:

我想将iron-form 的字段值双向数据绑定到 Firebase 节点(例如表示用户定义的设置)。

设置.html
<dom-module id="my-settings">
  <template>
    <firebase-document location="[[firebaseUrl]]"
                       data="{{form}}">
    </firebase-document>
    <form is="iron-form" id="form">
      <paper-checkbox id="history"
                      name="history"
                      on-change="_computeForm"
                      >Save history
      </paper-checkbox>
      <!-- ... -->
      <!-- All types of form fields go here -->
      <!-- ... -->
    </form>
  </template>
  <script>
    (function() {
      Polymer({
        is: 'my-settings',
        _computeForm: function() {
          this.form = this.$.form.serialize();
        }
      });
    })();
  </script>
</dom-module>

此表格需要:

  • 在更改时将其状态保持到 firebase(即,first-way 绑定 - 客户端到 firebase)和
  • 在加载时将表单字段设置为其保存的值(即,second-way 绑定 - firebase 到客户端 - 完成 two-way 绑定)。

问题

  1. 请提供实现此目的的最佳实践解决方案。

  2. 是否可以绑定整个表单(以声明方式?)并避免强制 在加载时独立设置每个表单字段值?

  3. 我鼓励使用伪代码或为我指明正确方向的概念。

【问题讨论】:

    标签: forms firebase polymer polymer-1.0 2-way-object-databinding


    【解决方案1】:

    iron-form 不是必需的。您需要将&lt;firebase-document&gt; 绑定到元素属性(表示表单)并将表单字段值绑定到该属性的子属性。

    Also, see this todo-list example.

    设置.html
    <dom-module id="my-settings">
      <template>
        <firebase-document location="[[firebaseUrl]]" data="{{form}}"></firebase-document>
        <paper-checkbox checked="{{form.history}}">Save history</paper-checkbox>
        <paper-input value="{{form.name}}" label="Name"></paper-input>
        <!-- Other form fields go here -->
      </template>
      <script>
        (function() {
          Polymer({
            is: 'my-settings',
            properties: {
              form: {
                type: Object,
                notify: true
              }
            }
          });
        })();
      </script>
    </dom-module>
    

    【讨论】:

    • 验证呢?假设您要验证 form.name
    • @integratingweb 您可以使用纸张输入的模式或 allowedPattern 属性进行验证。请参阅 elements.polymer-project.org 上的演示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多