简体   繁体   English

如果或声明的话

[英]ember if or statement

When using a conditional in ember, is it possible to have an OR ? 在ember中使用条件时,是否可以进行OR

{{#if foo OR bar}}

or 要么

{{#if foo || bar}}

There doesn't seem to be anything on it in the docs . 文档中似乎没有任何内容。

You should move the logic to your controller 您应该将逻辑移动到控制器

App.SomeController = Em.Controller.extend({
  foo: true,
  bar: false,

  fooOrBar: Em.computed.or('foo', 'bar')
});

Leaving the template logic to a minimum 将模板逻辑保持在最低限度

{{#if fooOrBar}}

Use https://github.com/jmurphyau/ember-truth-helpers : 使用https://github.com/jmurphyau/ember-truth-helpers

ember install ember-truth-helpers

Then you can say 然后你可以说

{{#if (or foo bar)}}

Depending on your perspective, Kingpin2k's answer is a bit out of date. 根据您的观点, Kingpin2k的答案有点过时了。 Previously, the community's understanding was that templates should be largely free of logic. 以前,社区的理解是模板应该基本上没有逻辑。 Overtime, our viewpoint has shifted towards putting more declarative logic in templates-- ember-composable-helpers is a great example of this. 加班,我们的观点已转向在模板中加入更多的声明性逻辑 - ember-composable-helpers就是一个很好的例子。

You can create a custom Handlebar helper function to check for conditional operators. 您可以创建自定义Handlebar辅助函数来检查条件运算符。

Ember.Handlebars.registerHelper('ifCond', function (temp_v1, operator, temp_v2, options) {
var v1,v2;
v1 = Ember.Handlebars.get(this, temp_v1, options);
v2 = Ember.Handlebars.get(this, temp_v2, 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);
    default:
        return options.inverse(this);
}
});

You can call it in your templates as 您可以在模板中将其称为

{{#ifCond foo '||' bar}}
    <div>Conditional helpers works </div>
{{/ifCond}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM