bodley
bodley

Reputation: 187

Call a function from one polymer component to another

I use Polymer, Html and Javascript.

I want to find any easy way to call a function from one polymer component to another. Let me describe what I want to achieve.

I have polymer component myComponent. In this component container with button called start:

<button on-click="_start" id="start">XyZ</button>

and also second component (let's say it is child component, because it is in myComponent, where I have got function _addNew in <script> section.

Now I want to fire _addNew function by clicking button with #start id. My question is how can I call a function from one polymer component when this function is placed in another?

I hope you understand me.

Upvotes: 1

Views: 3259

Answers (2)

bodley
bodley

Reputation: 187

In my case this is not working, because this.fire is going "up", to the parent component, not to a child component.

In my case this one help:

this.$$('component-name').function-name();

Upvotes: 0

Jeremy
Jeremy

Reputation: 178

if I understood correctly, basically what you want to do is fire from one component and listen in the other one.

So you fire from myComponent, in the _start method like:

this.fire('add:new');

and then to listen to the add:new event in your other component by adding a listener, for this specific event name:

listeners: {
   'add:new': '_addNew'
},

if it's not explicit enough, please do read the Polymer documentation it's a very nice and well explained documentation.

Hope it helps.

Upvotes: 2

Related Questions