代码之家  ›  专栏  ›  技术社区  ›  user4893295

在父上下文中查找布尔值

  •  0
  • user4893295  · 技术社区  · 7 年前

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

    血红蛋白

    {{#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”,但我不清楚它是如何应用于这个用例的?

    1 回复  |  直到 7 年前
        1
  •  0
  •   bobbyrne01    7 年前

    指定 this 在父上下文中: ../this 使用 id 作为查找键:

    lookup ../this id
    

    您需要将查找放在括号中,这样就可以使用 if ,根据您的要求,因此:

    {{#if (lookup ../this id) }} Yes! {{else}} No. {{/if}}