聚合物 - 使用计算绑定的多个手风琴

时间:2016-09-28 07:58:41

标签: javascript polymer polymer-1.0

我试图在我的应用程序中复制多次铁坍塌,就像示例一样,但遇到了问题: https://elements.polymer-project.org/elements/iron-collapse?view=demo:demo/index.html&active=iron-collapse

我得到的错误是:

[my-app::_createEventHandler]: listener method `_expandClicked('#collapse1')` not defined

标记

<a>
    <span>Parent Link 1</span>
    <paper-icon-button icon="expand-more" role="button" on-click="_expandClicked('#collapse1')"></paper-icon-button>
</a>
<iron-collapse id="collapse1">
    <a class="link" href="/view2">Sport F</a>
    <a class="link" href="/view3">Child C</a>
</iron-collapse>

功能

_expandClicked: function(selector) {
    document.querySelector(selector).toggle();
    // Don't propagate to the anchor link.
    event.preventDefault();
    event.stopImmediatePropagation();
}

有什么想法吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

设置annotated event listener时,该值必须仅为方法名称。您可以将函数参数作为事件监听器可以读取的data attribute传递。

// template
<paper-button on-tap="_toggleCollapse" data-selector="#collapse1">

// script
_toggleCollapse: function(e) {
  const selector = e.target.dataset.selector;
  this.$$(selector).toggle();
}

HTMLImports.whenReady(() => {
  "use strict";

  Polymer({
    is: 'x-foo',
    _toggleCollapse: function(e) {
      // Assume e.target is the paper-button or an immediate child
      const selector = e.target.dataset.selector || e.target.parentElement.dataset.selector;
      if (selector) {
        this.$$(selector).toggle();
      }
    }
  });
});
<head>
  <base href="https://polygit.org/polymer+1.6.1/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  
  <link rel="import" href="paper-button/paper-button.html">
  <link rel="import" href="iron-collapse/iron-collapse.html">
  <link rel="import" href="iron-icons/iron-icons.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <style>
        :host {
          font-family: Roboto, Noto, sans-serif;
        }

        section {
          margin-top: 20px;
        }

        paper-button {
          background: #eee;
          width: 100%;
          text-transform: none;
          justify-content: flex-start;
          font-size: 18px;
        }
        
        .content {
          margin-left: 6px;
          padding: 15px;
          border: 1px solid #dedede;
          width: calc(100% - 34px);
          border: 1px solid #dedede;
          border-top: none;
          border-bottom-left-radius: 5px;
          border-bottom-right-radius: 5px;
          box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
        }
      </style>
      <section>
        <paper-button on-tap="_toggleCollapse" data-selector="#collapse1">
          <span>Collapse #1</span>
          <iron-icon icon="expand-more"></iron-icon>
        </paper-button>
        <iron-collapse id="collapse1">
          <div class="content">
            Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea, id minim maiestatis incorrupte duo. Dolorum verterem ad ius, his et nullam verterem. Eu alia debet usu, an doming tritani est. Vix ad ponderum petentium suavitate, eum eu tempor populo, graece sententiae constituam vim ex. Cu torquatos reprimique neglegentur nec, voluptua periculis has ut, at eos discere deleniti sensibus. Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea, id minim maiestatis incorrupte duo. Dolorum verterem ad ius, his et nullam verterem. Eu alia debet usu, an doming tritani est. Vix ad ponderum petentium suavitate, eum eu tempor populo, graece sententiae constituam vim ex. Cu torquatos reprimique neglegentur nec, voluptua periculis has ut, at eos discere deleniti sensibus.
          </div>
        </iron-collapse>
      </section>
      <section>
        <paper-button on-tap="_toggleCollapse" data-selector="#collapse2">
          <span>Collapse #2</span>
          <iron-icon icon="expand-more"></iron-icon>
        </paper-button>
        <iron-collapse id="collapse2">
          <div class="content">
            Pro saepe pertinax ei, ad pri animal labores suscipiantur. Modus commodo minimum eum te, vero utinam assueverit per eu, zril oportere suscipiantur pri te. Partem percipitur deterruisset ad sea, at eam suas luptatum dissentiunt. No error alienum pro, erant senserit ex mei, pri semper alterum no. Ut habemus menandri vulputate mea. Feugiat verterem ut sed. Dolores maiestatis id per. Pro saepe pertinax ei, ad pri animal labores suscipiantur. Modus commodo minimum eum te, vero utinam assueverit per eu, zril oportere suscipiantur pri te. Partem percipitur deterruisset ad sea, at eam suas luptatum dissentiunt. No error alienum pro, erant senserit ex mei, pri semper alterum no. Ut habemus menandri vulputate mea. Feugiat verterem ut sed. Dolores maiestatis id per.
          </div>
        </iron-collapse>
      </section>
    </template>
  </dom-module>
</body>

codepen