gpt4 book ai didi

ember.js - 在Ember.js的模板中,当您在#each block 中时,如何在父上下文中引用值?

转载 作者:行者123 更新时间:2023-12-04 03:09:04 25 4
gpt4 key购买 nike

我在模板中遇到一种情况,我想在每个块内的父上下文中的值上使用if块。

编码:

App = Ember.Application.create({});

App.view = Ember.View.extend({
foo: [1, 2, 3],
bar: true
});

模板:
<script type="text/x-handlebars">
{{#view App.view}}
{{#each foo}}
{{#if bar}}
{{this}}
{{/if}}
{{/each}}
{{/view}}
</script>

这是行不通的,因为每个循环中引用的名称的作用域是迭代元素。您如何在父级上下文中指代事物?

演示: http://jsfiddle.net/hekevintran/sMeyC/1/

最佳答案

我找到了更好的解决方案。
从Ember.js View Layer指南(http://emberjs.com/guides/understanding-ember/the-view-layer/):

Handlebars helpers in Ember may also specify variables. For example, the {{#with controller.person as tom}} form specifies a tom variable that descendent scopes can access. Even if a child context has a tom property, the tom variable will supersede it.

This form has one major benefit: it allows you to shorten long paths without losing access to the parent scope.

It is especially important in the {{#each}} helper, which provides a {{#each person in people}} form. In this form, descendent context have access to the person variable, but remain in the same scope as where the template invoked the each.


模板:
<script type="text/x-handlebars" >
{{#view App.view}}
{{#each number in view.foo}}
{{#if view.bar}}
{{number}}
{{/if}}
{{/each}}
{{/view}}
</script>​
演示: http://jsfiddle.net/hekevintran/hpcJv/1/

关于ember.js - 在Ember.js的模板中,当您在#each block 中时,如何在父上下文中引用值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10662353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com