With the conventional use of a dom-repeat loop and a template, the field reference is hard-coded against the source object. In the example below we're pulling the name and description fields from a JSON object. Works fine.
<template is="dom-repeat" items="[[subjects]]">
{{item.name}}, {item.description}}
</template>
In my application I'd like to pull values programmatically by using a nested template that loops through a supplied list of fields. However I'm not able to make it work, the results come out as literal text rather than performing as I'd like:
<template is="dom-repeat" items="[[subjects]]">
<template is="dom-repeat" items="[[fields]]" as="field">
{{item.{{field}}}},
</template>
</template>
These are the variations I've tried and the results using 'name' and 'description' as the fields:
{{item.{{field}}}}, -> "{{item.name}} {{item.description}}"
{{item[ {{field}} ]}}, -> "{{item[ name ]}} {{item[ description ]}}"
Ideally, I would like it to work like this:
someFunction( {{item}}, {{field}} )
Where someFunction would take in the object & field specifier and return a string.
Just not sure how to make it happen. Any ideas?
Addendum showing missing parts called out:
<iron-ajax>
auto
url="https://api.github.com/users/burczu/repos"
params='{"type":"all"}'
handle-as="json"
on-response="handleResponse">
</iron-ajax>
and
<script>
class MyElement extends Polymer.Element {
static get is() { return 'my-element'; }
static get properties() {
return {
subjects: { type: Array },
fields: { type: Object }
};
}
ready() {
super.ready();
this.fields = JSON.parse('{"show": ["name", "description"] }').show;
}
handleResponse(data) {
this.subjects = data.detail.response;
}
}
window.customElements.define(MyElement.is, MyElement);
</script>