Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send object with on-click functions of Polymer

I'm trying to send object as params of on-click functions, I already saw some posts using data-xxx to send values and retrieving them with the target.attributes field but that works only for strings and not objects.

Here is a jsbin showing the problem: http://jsbin.com/tujekilafowa/1/edit?html,console,output

There is a solution, even though its terrible, by using JSON.stringify to send the data and retrieving them with JSON.parse: http://jsbin.com/lavocacadoti/1/edit?html,console,output

Is there a better way?

In a perfect world, I would like it to work as Angular by passing parameters to a function directly inside the HTML call...

EDIT

I opened an issue on polymer project about this problem.

like image 565
pikanezi Avatar asked Mar 19 '23 17:03

pikanezi


1 Answers

Taking Peters answer above, this works for me (if it is enough to access the model)...

Polymer Element Template:

<template>
    <template repeat="{{ address in addresses }}">
        <paper-button label="-" on-click="{{ callback }}">less</paper-button>
    </template>
</template>

The Polymer Callback looks like this:

Polymer({
    callback: function (event, detail, sender) {
        console.log(sender.templateInstance.model.address);
    }
});
like image 66
Christopher Avatar answered Mar 29 '23 10:03

Christopher