【问题标题】:Two way data binding with Polymer.Templatizer使用 Polymer.Templatizer 的两种方式数据绑定
【发布时间】:2016-03-09 20:34:07
【问题描述】:

我正在尝试使用模板化器在 Polymer 中的宿主元素和模板之间进行两种数据绑定。例如,如果我试图让两个输入框保持同步:

<html>
  <body>
    <my-element>
      <template >
        <input type="text" value="{{test::change}}" />
        <div>The value of 'test' is: <span>{{test}}</span></div>
      </template>
    </my-element>

   <dom-module id="my-element">
      <template>
         <input type="text" value="{{test::change}}" />
           value:
           <p>{{test}}</p>
           <div id="items"></div>
           <content id="template"></content>
      </template>
   </dom-module>

   <script>

    Polymer({
      is: 'my-element',
      test: {
        type: String,
        value: "a"
      },
     behaviors: [ Polymer.Templatizer ],
     _forwardParentProp: function(prop, value) {debugger},
     _forwardParentPath: function(path, value) {debugger},
     _forwardInstanceProp: function(inst, prop, value) {debugger},
     _forwardInstancePath: function(inst, path, value) {debugger},
     ready: function() {
     this._instanceProps = {
       test: true
     };
     var templates = Polymer.dom(this.$.template).getDistributedNodes();
     template = templates[1];
     this.templatize(template);
     var itemNode = this.stamp({ test: this.test});
     Polymer.dom(this.$.items).appendChild(itemNode.root);
    }
   });

   </script>
  </body>
</html>

在上面的代码中,我点击了 _forwardInstanceProp 中的调试器,但没有点击任何其他代码。为什么是这样?在 _forwardInstanceProp 中,我可以访问 my-element 并手动更新测试属性。有一个更好的方法吗?我还可以将 my-element 上的观察者添加到 test 属性,然后将 my-element 中的任何更改传播到模板。有没有更好的方法来做到这一点?我只是想了解所有这四种方法的作用以及何时/为什么应该使用它们。

【问题讨论】:

    标签: polymer


    【解决方案1】:

    这让我无法让_forwardParentPath 和_forwardParentProp 运行。但是,我知道其他两个什么时候运行 :)

    _forwardInstanceProp 运行传递给 stamp 的模型的直接属性 并且 _instanceProps 被初始化:

    this._instanceProps = {
      text: true
    };
    
    var clone = this.stamp({
      text: this.text
    });
    

    另一方面,_forwardInstancePath 在您将嵌套对象传递给stamp 时运行:

    var clone = this.stamp({
      nested: {
        text: this.text
      }
    });
    

    请参阅此垃圾箱以获取示例:http://jsbin.com/kipato/2/edit?html,js,console,output

    在标记的模板中,有两个输入绑定到触发 instanceProp 和 instancePath 的两个变量。不幸的是,我无法修复后者发生时引发的错误。

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多