在父上下文中查找布尔值



我试图根据父上下文中是否存在值为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}}

最新更新