Polymer 1.0中自动绑定模板中的计算绑定

es_*_*ode 6 polymer polymer-1.0

如何在自动绑定模板(即声明为<template is='dom-bind'>...</template>)中定义计算绑定?

Ada*_*ian 6

只需通过脚本将计算出的绑定直接分配给模板元素,确保在计算出的绑定定义后初始化所涉及的属性.

例:

<template is="dom-bind">
  <div>
    <input value="{{text::input}}">
  </div>
  <div>[[describe(text)]]</div>
</template>

<script>
  (function() {
    var template = document.querySelector('template[is="dom-bind"]');

    template.describe = function(text) {
      if (text) {
        return 'You entered "' + text + '", which is ' + text.length + ' characters long.';
      } else {
        return 'You didn\'t even enter a thing! Shame on you.';
      }
    };

    template.text = '';
  })();
</script>
Run Code Online (Sandbox Code Playgroud)