Is there a way to pass an argument to a Polymer function from an element attribute inside its <template>
?
<script src="http://www.polymer-project.org/1.0/samples/components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="http://www.polymer-project.org/1.0/samples/components/polymer/polymer.html" />
<dom-module id="example-element">
<template>
...
<paper-button id="foo" on-tap="bar">Click</paper-button>
...
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'example-element',
properties: {...},
bar: function(arg){
// Do stuff using argument arg
}
});
})();
</script>
Background Research
I have combed through the documentation which appears silent on the matter. It doesn't say whether you can or can not. But when I try it, it fails. But maybe I'm not doing it correctly. So I need some assistance.
The only thing I have come across is event listeners which doesn't seem to be able to take the arguments I want to pass. Say, an id
or a name
.
Previous Attempts
I have tried (unsuccessfully) doing things like:
<paper-button id="foo" on-tap="bar('foo')"> Click </paper-button>
but nothing seems to work.
The event listeners idea doesn't work because they limit the arguments and I can't get the, say, id
I need.
You could utilize HTML5 data attributes instead. Try like this:
Shining more light on a subtle difference mentioned in the comments above.
Notice
$=
must be used if reading a data binding.Inside
dom-repeat
, theitem
(or whichever name you give it) is available ate.model.item
.Just now I got this to work:
Depending on the situation, the clean way to do this is usually using dom-repeat. If you can format your data as an array of objects, you can just use e.model to get everything.
After searching a lot, I found what I think is the cleanest possible solution.
If the paper-button is inside a template, e.g.
Then the properties can be accessed via "model" property in event object passed to function.
Is there a way to pass an argument to a Polymer function from an attribute of an element inside its
<template>
.Instead of using an event use a computed binding. Computed bindings can accept literal strings.
Checkout the working example below. In this example a button can be hidden based on the parameter that is passed.
Note: You can get the
id
or other attributes of an element that an event is run on throughevent.target
. If you are only looking for other attributes as parameters this might also be a valid solution.