我正在使用自定义的工具栏注册助手来检查条件运算符(&,\调用在我的模板中的每个帮助器中的寄存器助手,这个模板由一个来自路由的模型支持。
但是,当我将值从模型传递给自定义助手时,该值不会传递给助手,而是传递字符串本身。
模板:
<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注册助手:
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 -> 链接
发布于 2014-06-04 09:47:26
如果要检索对象值,则需要获取它。
v1 = Ember.Handlebars.get(this, v1, options);https://stackoverflow.com/questions/24033078
复制相似问题