我希望能够使用<core-ajax>
渲染一个远程集合:
<rendered-collection url="/api/items">
<custom-element value="{{ _it_ }}"></custom-element>
</rendered-collection>
其中<rendered-collection>
类似于:
<link rel="import" href="/core-ajax/core-ajax.html">
<polymer-element name="rendered-collection" attributes="url" noscript>
<template>
<core-ajax url="{{ url }}" response="{{ collection }}" auto handleAs="json"></core-ajax>
<template repeat="{{ _it_ in collection }}">
<content><!-- cannot be used like that unfortunately --></content>
</template>
</template>
</polymer-element>
我意识到这不是<content>
应该如何工作的,而且我仍然需要将模型注入其中。
我见过answers建议在JS中检索内容的节点:
<style>
::content > * {
display: none;
}
</style>
<content id="content"></content>
...
<script>
Polymer('rendered-collection', {
attached: function () {
this.contentNodes = this.$.content.getDistributedNodes();
// then...how do I inject models from the collection into the nodes?
}
});
</script>
最好的方法是什么?
答案 0 :(得分:2)
如果我正确理解您的用例,您希望<rendered-collection>
的子项描述集合中每个项目的呈现。这正是<template>
的用途。因此,如果我们建议使用<rendered-collection>
,那么:
<rendered-collection>
<template>
<h2>{{name}}</h2>
</template>
</rendered-collection>
然后我们可以使用一些模板来渲染它:
<polymer-element name="rendered-collection">
<template>
<content></content>
</template>
<script>
Polymer('rendered-collection', {
collection: [
{name: 'alpha'},
{name: 'beta'},
{name: 'gamma'}
],
ready: function() {
this.bindTemplate();
},
bindTemplate: function() {
// user-supplied template
var t = this.querySelector('template');
// optional, but supplies fancy expression support
t.bindingDelegate = new PolymerExpressions();
// repeat over the entire model
t.setAttribute('repeat', '{{}}');
// set the model to our collection
t.model = this.collection;
}
});
</script>
</polymer-element>
答案 1 :(得分:0)
下面的代码似乎有效,但我希望对此解决方案有反馈意见,这种解决方案感觉很糟糕且可以改进:
<rendered-collection url="/api/items">
<custom-element value="{{ _it_ }}"></custom-element>
</rendered-collection>
与
<polymer-element name="html-render" attributes="html model">
<template></template>
<script>
Polymer({
htmlChanged: function() {
var tmpl = document.createElement("template");
tmpl.setAttribute("bind", "");
// WARNING: Potential XSS vulnerability
tmpl.innerHTML = this.html;
tmpl.model = { _it_: this.model };
this.shadowRoot.appendChild(tmpl);
// Needed ?
Platform.performMicrotaskCheckpoint();
}
});
</script>
</polymer-element>
<polymer-element name="rendered-collection" attributes="url">
<template>
<content id="content"></content>
<core-ajax url="{{ url }}" response="{{ collection }}" auto handleAs="json"></core-ajax>
<template repeat="{{ item in collection }}">
<template repeat="{{ node in contentNodes }}">
<html-render html="{{ node.outerHTML }}" model="{{ item }}"></html-render>
</template>
</template>
</template>
<script>
Polymer({
attached: function () {
var nodes = this.$.content.getDistributedNodes();
this.contentNodes = _.isArray(nodes) ? nodes : nodes.array();
this.$.content.remove();
}
});
</script>
</polymer-element>
答案 2 :(得分:0)
使用Scott Miles&#39;解决方案,但包括<core-ajax>
:
<rendered-collection url="/api/items">
<template>
<custom-element value="{{ }}"></custom-element>
</template>
</rendered-collection>
与
<polymer-element name="rendered-collection" attributes="url">
<template>
<core-ajax auto
url="{{ url }}"
handleAs="json"
response="{{ collection }}"></core-ajax>
<content></content>
</template>
<script>
Polymer({
ready: function() {
this.clientTemplate = this.querySelector('template');
this.clientTemplate.bindingDelegate = new PolymerExpressions();
this.clientTemplate.model = this;
this.clientTemplate.setAttribute('repeat', '{{ collection }}');
}
});
</script>
</polymer-element>