简体   繁体   English

Ember HTMLBars内联组合条件

[英]Ember HTMLBars inline combined conditions

When trying to have a simple conditional style-class assignment in HTMLBars with Ember 1.13, the following code does a great job: 当尝试使用Ember 1.13在HTMLBars中进行简单的条件样式类分配时,以下代码可以很好地完成工作:

{{#each items as |item|}}
  <li class="tag {{if item.selected 'active' 'inactive'}}">{{item.key}}</li>
{{/each}}

Still, is there a way to combine conditions in the upper case, like checking for another condition? 还有,有没有办法在大写形式下组合条件,例如检查另一个条件? Something like the following code ... 类似于以下代码...

{{#each items as |item|}}
  <li class="tag {{if (item.selected or noneSelected) 'active' 'inactive'}}">{{item.key}}</li>
{{/each}}

... or is the only way to achieve a check for several conditions via an Ember helper? ...还是通过Ember助手检查多个条件的唯一方法?

Thanks for your support! 谢谢你的支持!

You will want to make your own helper let's call it conditional-or : 您将希望使自己的助手成为conditional-or

import Ember from 'ember';

const { Helper: { helper } } = Ember;

export function conditionalOr(conditions, {valid, invalid}) {
  invalid = invalid || '';
  return conditions.some(elem => elem) ? valid : invalid;
}

export default helper(conditionalOr);

What some() does is iterate over the array elements and return true when one meets the criteria. some()所做的是遍历数组元素,并在满足条件时返回true。

And you will be able to use it in your template like so: 您将可以在模板中使用它,如下所示:

<li class="tag {{conditional-or item.selected noneSelected valid='active' invalid='inactive'}}">{{item.key}}</li>

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

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