Rhys Rhys - 2 months ago 14
Javascript Question

Polymer - Multiple accordions using computed bindings

I am trying to replicate the multiple iron-collapses in my app like the example but running into problems:
https://elements.polymer-project.org/elements/iron-collapse?view=demo:demo/index.html&active=iron-collapse

The error I get is:

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


The Markup

<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>


The Function

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


Any ideas?
Thanks!

Answer

When setting up an annotated event listener, the value must be the method name only. You could pass a function argument as a data attribute that your event listener could read.

// 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

Comments