In templates in Ember.js, how do you refer to a value in the parent context when you are inside an #each block?

前端 未结 4 765
栀梦
栀梦 2020-12-25 08:58

I have a situation in a template where I want to use an if block on a value in the parent context while inside an each block.

The code:

App = Ember.A         


        
4条回答
  •  太阳男子
    2020-12-25 09:15

    I found a better solution.

    From the Ember.js View Layer guide (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.

    The template:

    Demo: http://jsfiddle.net/hekevintran/hpcJv/1/

提交回复
热议问题