1

I'm trying to call a function and pass a parameter but it gives error that function is not defined. I suppose it's not the proper way to pass argument.

//it's a big component with many methods

updateData: function(id) {
  console.log(id);
  }
<div class="basic">
  <preview-list class="profileList">
    <template is="dom-repeat" items="{{ followers }}">
      <preview-profile profile$="{{ item }}">
        <div class="connectWrapper">
          <template is="dom-if" if="{_computeIsNotConnected(item.isConnection, item.isConnection2)}}">
            <custom-button on-tap="[[updateData(item.id)]]"></custom-button>
          </template>
        </div>
      </preview-profile>
    </template>
  </preview-list>
</div>

It shows the following error : listener method `[[updateData(item.id)]]` not defined I tried with curly braces too. How could I pass parameter to a function?

ivva
  • 2,819
  • 9
  • 31
  • 64

2 Answers2

2

You must use the name of your function and get your object from event parameter:

<template is="dom-repeat" items="{{ data }}">
  <custom-button on-tap="updateData"></custom-button>
</template>

And define your event handler like this:

updateData: function(event) {
  console.log(event.model.item.id);
}

Update

as you are using dom-if inside dom-repeat, it's a bug (https://github.com/Polymer/polymer/issues/2574)

you can either use hidden$="{{_computeIsNotConnected(item.isConnection, item.isConnection2)}}" instead of dom-if template,

Or use this code snippet instead:

<template id="repeater" is="dom-repeat" items="{{ data }}">
  <custom-button on-tap="updateData"></custom-button>
</template>

updateData: function(event) {
  console.log(this.$.repeater.modelForElement(event.target));
}
Shirin Safaeian
  • 950
  • 6
  • 18
1

Polymer doesn't want you to pass an argument here. Instead you should access the data you need via the model... e.model.get('item.id'). See this answer for more: https://stackoverflow.com/a/40205828/2718998

Community
  • 1
  • 1
Tim Mickey
  • 361
  • 3
  • 10