MustacheJS - 按属性访问值作为索引

时间:2017-07-06 13:38:00

标签: javascript templates mustache

范围:

let bases = [{id: "51667c34ed4dd", name: "CAYENNE", sort: 1}, {id: "516682b6a160e", name: "FORT DE FRANCE", sort: 2}, {id: "516682c230fbb", name: "POINT A PITRE", sort: 3}];
let filters = {bases: {51667c34ed4dd: true, 516682b6a160e: false, 516682c230fbb: true}, aircrafts: {}};
let set = {bases, filters};

模板:

{{#bases}}
    <input type="checkbox" class="base_checkbox" value="1" id="{{id}}" name="{{name}}" {{(if checked)}}checked="checked"{{/(if checked)}}>
{{/bases}}

用法:

Mustache.render(template, set);

{{#bases}}循环中,如何以当前范围属性作为键访问另一个外部对象值?

喜欢:{{filters.bases {{id}} }}其中{{id}}是循环中当前对象的属性。

1 个答案:

答案 0 :(得分:0)

已解决:

bases.forEach(b => {
    b.checked = () => {
        return filters["bases"][b.id];
    }
});

Mustache使用函数返回值作为变量。