【发布时间】: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