【问题标题】:How to get parent data inside an Handlebars Helper scope?如何在 Handlebars Helper 范围内获取父数据?
【发布时间】:2014-06-17 14:46:33
【问题描述】:

我正在尝试生成一个选项集,我可以在其中选择所选选项,但是当我像上面示例中那样嵌套 Helpers 时,我无法访问 timeSpent

这就是我将数据发送到车把模板的方式:

{
    feed : {
        foodMinutes : 90,
        timeSpent : 72
    }
}

这是我生成选择选项的地方,我认为有一种类似../timeSpent 的方式来获取父数据,但它不起作用:

<select>
    {{#count feed.foodMinutes 1 0}}
    {{#equal ../timeSpent this}}
    <option value="{{this}}" selected>{{this}} minuti</option>
    {{else}}
    <option value="{{this}}">{{this}} minuti</option>
    {{/equal}}
    {{/count}}
</select>

这些是我的助手:

Handlebars.registerHelper("count", function(count, step, start, block) {
    var accum, steps, startFrom;
    accum = "";
    steps = step || 1;
    startFrom = start || 0;

    for(var i = startFrom; i < count; i += steps) {
        accum += block.fn(i);
    }
    return accum;
});

Handlebars.registerHelper("equal", function(lvalue, rvalue, options) {
    if (arguments.length < 3) {
        throw new Error("Handlebars Helper equal needs 2 parameters");
    }
    if (lvalue != rvalue) {
        return options.inverse(this);
    } else {
        return options.fn(this);
    }
});

那么如何在 Handlebars Helper 范围内获取父数据?

【问题讨论】:

    标签: handlebars.js parent


    【解决方案1】:

    解决方案很简单,我用../feed.timeSpent 代替../timeSpent

    <select>
        {{#count feed.foodMinutes 1 0}}
        {{#equal ../feed.timeSpent this}}
        <option value="{{this}}" selected>{{this}} minuti</option>
        {{else}}
        <option value="{{this}}">{{this}} minuti</option>
        {{/equal}}
        {{/count}}
    </select>
    

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 2015-07-02
      • 1970-01-01
      • 2013-10-03
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      相关资源
      最近更新 更多