在父上下文中查找布尔值
Looking up a boolean in parent context
我正在尝试根据父上下文中是否存在值为 true 的 属性 来显示内容。
所以:
js
var products = [ {id: "a", desc: "Product A"},
{id: "b", desc: "Product B"} ]
var customers = [ {name: "John", a: true, b: true},
{name: "Mike", a: false, b: true} ]
hbs
{{#each customers}}
Name: {{name}}
{{#each ../products}}
Has product {{desc}}: {{#if // something // }} Yes! {{else}} No. {{/if}}
{{/each}}
{{/each}}
如果这有意义?在纯 js 中,我会使用类似的东西:
for(var c=0; c<customers.length; c++) {
let customer = customers[c];
for(var p=0; i<products.length; p++) {
let product = products[p];
let hasThisProduct = customer[product] ? 'yes!' : 'no.';
[....]
}
}
根据我之前阅读的内容,我认为我应该使用 'lookup',但我不太清楚它如何适用于这个用例?
在父上下文中指定 this
:../this
并使用 id
作为查找键:
lookup ../this id
您需要将查找放在括号中,这样您就可以根据您的要求使用 if
测试返回值,因此:
{{#if (lookup ../this id) }} Yes! {{else}} No. {{/if}}
我正在尝试根据父上下文中是否存在值为 true 的 属性 来显示内容。
所以:
js
var products = [ {id: "a", desc: "Product A"},
{id: "b", desc: "Product B"} ]
var customers = [ {name: "John", a: true, b: true},
{name: "Mike", a: false, b: true} ]
hbs
{{#each customers}}
Name: {{name}}
{{#each ../products}}
Has product {{desc}}: {{#if // something // }} Yes! {{else}} No. {{/if}}
{{/each}}
{{/each}}
如果这有意义?在纯 js 中,我会使用类似的东西:
for(var c=0; c<customers.length; c++) {
let customer = customers[c];
for(var p=0; i<products.length; p++) {
let product = products[p];
let hasThisProduct = customer[product] ? 'yes!' : 'no.';
[....]
}
}
根据我之前阅读的内容,我认为我应该使用 'lookup',但我不太清楚它如何适用于这个用例?
在父上下文中指定 this
:../this
并使用 id
作为查找键:
lookup ../this id
您需要将查找放在括号中,这样您就可以根据您的要求使用 if
测试返回值,因此:
{{#if (lookup ../this id) }} Yes! {{else}} No. {{/if}}