把手注册助手 - 从模型发送值

时间:2014-06-04 08:58:48

标签: ember.js handlebars.js

我正在使用自定义把手寄存器帮助程序来检查条件运算符(&&,||,==)并在我的每个帮助程序中调用寄存器帮助程序,该模板由Route中的模型支持。

但是当我将值从模型传递给自定义帮助器时,该值不会传递给帮助程序,而是传递字符串。

模板:

<script type="text/x-handlebars" data-template-name="index">
    <ul>
    {{#each item in model}}
        {{#ifCond item '==' 'red'}}
            <li>{{item}}</li>
        {{/ifCond}}
    {{/each}}
    </ul>
</script>

路线:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return ['red', 'yellow', 'blue', 'red', 'blue', 'pink'];
    }
});

ifCond Register Helper:

Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
    console.log(v1); //item - Here the value is coming as such and not the model value.
    console.log(v2);  // red
    console.log(operator); // ==
    console.log(options);
    switch (operator) {
    case '==':
        return (v1 == v2) ? options.fn(this) : options.inverse(this);
    case '===':
        return (v1 === v2) ? options.fn(this) : options.inverse(this);
    case '<':
        return (v1 < v2) ? options.fn(this) : options.inverse(this);
    case '<=':
        return (v1 <= v2) ? options.fn(this) : options.inverse(this);
    case '>':
        return (v1 > v2) ? options.fn(this) : options.inverse(this);
    case '>=':
        return (v1 >= v2) ? options.fn(this) : options.inverse(this);
    case '&&':
        return (v1 && v2) ? options.fn(this) : options.inverse(this);
    case '||':
        return (v1 || v2) ? options.fn(this) : options.inverse(this);
    default:
        return options.inverse(this);
    }
});

如何将模型中的值传递给自定义帮助程序。

JSBin - &gt; Link

1 个答案:

答案 0 :(得分:0)

如果要检索对象值,则需要获取它。

v1 = Ember.Handlebars.get(this, v1, options);
相关问题