将#each 帮助程序的命名参数风格与通用部分一起使用
Using named-parameter flavor of #each helper together with a generic partial
在我的应用程序中,我有不同的节点类型,它们共享相似的结构。为此,我使用这样的模板:
{{#each model}}
<td>{{name}}</td>
... {{! more properties specific to this node (agent) }}
{{partial "common/output_cells"}}
{{/each}}
common/output_cells
为:
{{#if controller.startnodeAllowed}}<td>{{#if outputs§startnode}}{{#link-to startNodeRoute outputs§startnode}}{{view "nodeIcon" nodeIdBinding="outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
现在我正在过渡到 named-parameter helpers,所以我有:
{{#each agent in content}}
<td>{{agent.name}}</td>
... {{! more properties specific to this node (agent) }}
{{partial "common/output_cells"}}
{{/each}}
而且我被迫更改公共部分:
{{#if controller.startnodeAllowed}}<td>{{#if agent.outputs§startnode}}{{#link-to agent.startNodeRoute agent.outputs§startnode}}{{view "nodeIcon" nodeIdBinding="agent.outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
但这使得我的 common/output_cells
部分绑定到一个代理,而它应该绑定到一个通用节点。是否可以像下面这样调用partial(语法是我编的,因为我不知道怎么做)?
{{partial "common/output_cells" with agent as node}}
这样我就可以以更通用的方式实现部分:
{{#if controller.startnodeAllowed}}<td>{{#if node.outputs§startnode}}{{#link-to node.startNodeRoute node.outputs§startnode}}{{view "nodeIcon" nodeIdBinding="node.outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
如果这不可能,我还有哪些其他选择可以重用部分?
尝试使用 {{with}}
:
{{#each agent in content}}
<td>{{agent.name}}</td>
... {{! more properties specific to this node (agent) }}
{{#with agent}}
{{partial "common/output_cells"}}
{{/with}}
{{/each}}
换句话说,由于 {{#each}}
的这种风格不会改变上下文,所以请自己改变上下文,这正是 {{#with}}
所做的。
见http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_with。
在我的应用程序中,我有不同的节点类型,它们共享相似的结构。为此,我使用这样的模板:
{{#each model}}
<td>{{name}}</td>
... {{! more properties specific to this node (agent) }}
{{partial "common/output_cells"}}
{{/each}}
common/output_cells
为:
{{#if controller.startnodeAllowed}}<td>{{#if outputs§startnode}}{{#link-to startNodeRoute outputs§startnode}}{{view "nodeIcon" nodeIdBinding="outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
现在我正在过渡到 named-parameter helpers,所以我有:
{{#each agent in content}}
<td>{{agent.name}}</td>
... {{! more properties specific to this node (agent) }}
{{partial "common/output_cells"}}
{{/each}}
而且我被迫更改公共部分:
{{#if controller.startnodeAllowed}}<td>{{#if agent.outputs§startnode}}{{#link-to agent.startNodeRoute agent.outputs§startnode}}{{view "nodeIcon" nodeIdBinding="agent.outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
但这使得我的 common/output_cells
部分绑定到一个代理,而它应该绑定到一个通用节点。是否可以像下面这样调用partial(语法是我编的,因为我不知道怎么做)?
{{partial "common/output_cells" with agent as node}}
这样我就可以以更通用的方式实现部分:
{{#if controller.startnodeAllowed}}<td>{{#if node.outputs§startnode}}{{#link-to node.startNodeRoute node.outputs§startnode}}{{view "nodeIcon" nodeIdBinding="node.outputs§startnode"}}{{/link-to}}{{else}}{{view "nodeIcon"}}{{/if}}</td>{{/if}}
... {{! other common properties for all nodes (lots of them)}}
如果这不可能,我还有哪些其他选择可以重用部分?
尝试使用 {{with}}
:
{{#each agent in content}}
<td>{{agent.name}}</td>
... {{! more properties specific to this node (agent) }}
{{#with agent}}
{{partial "common/output_cells"}}
{{/with}}
{{/each}}
换句话说,由于 {{#each}}
的这种风格不会改变上下文,所以请自己改变上下文,这正是 {{#with}}
所做的。
见http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_with。