访问父上下文handlebars.js中的object.param

时间:2017-10-18 10:49:05

标签: node.js handlebars.js

无法获取父对象的参数值

function checksum(str) {
    var hash = 5382,
        i = str.length;

    while (i--) hash = (hash * 33) ^ str.charCodeAt(i);

    return hash >>> 0;
}

我的对象是格式

{{#each myObject.details}}
  <span class="marginL15 pull-left hidden-xs">
    <a href="
      /rates
      /{{../port_data.name}}
      /{{../port_data.name}}
      /{{name}}
      /{{port_code}}
    ">
      {{name}}
    </a>
  </span>
{{/each}}

还尝试{ "meta_title": "fw", "port_data": { "display_name": "Rajkot (INRAJ), Rajkot, India", "name": "Rajkot", "port_code": "INRAJ" }, "details": [ { "_id": { "$oid": "58f04ef3c0a35f10b7cc08fa" }, "display_name": "Sokhna(Al Sokhna) (EGSOK), Suez, Egypt", "name": "Sokhna(Al Sokhna)", "port_code": "EGSOK" }, { "_id": { "$oid": "58ff42cfc0a35f493be68031" }, "display_name": "Rouyn Noranda Airport - CAYUY (YUY), Canada, usa", "name": "Rouyn Noranda Airport - CAYUY", "port_code": "YUY" }, { "_id": { "$oid": "58f1c6e9c0a35f2d16dd4f44" }, "display_name": "Sharjah (AESHJ), Sharjah, United Arab Emirates", "name": "Sharjah", "port_code": "AESHJ" } ] } 但没有用。

1 个答案:

答案 0 :(得分:1)

myObject.details传递给{{#each}}帮助程序时,只传递存储在details变量的myObject字段中的引用数组 - 无法查找存储在../中的东西,因为那里什么也没有。

你能做什么:

{{#myObject}}
  {{#each details}}
  <span class="marginL15 pull-left hidden-xs">
    <a href="/rates
      /{{../port_data.name}}
      /{{name}}
      /{{port_code}}
    ">
      {{name}}
    </a>
  </span>
  {{/each}}
{{/myObject}}

它使您可以访问myObject循环上方的整个{{#each}}变量。