在ember组件模板中,如何使用'if'来检查属性等于X的数组中的至少一个? [英] In an ember component template how can I use 'if' to check for at least one of an array having a property equal to X?

查看:163
本文介绍了在ember组件模板中,如何使用'if'来检查属性等于X的数组中的至少一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组件通过通知

通知有一个帐户,一个帐户有很多联系人

A notification has an account, and an account has many contacts.

所以我可以轻松地做一些像

So I could easily do something like

{{#if (is-greater-than notification.account.contacts.length 0)}}
  <p>stuff</p>
{{/if}}

要检查联系人的存在,但是什么如果我只想显示如果至少有一个联系人具有 firstName 'John',那么我该如何才能在组件模板中执行此操作?或者如果不可能在模板中什么是下一个最佳方法?

To check for the existence of a contact, but what if I wanted to only display if at least one contact has firstName 'John', how could I do this in the component template only? Or if not possible in the template what is the next best approach?

我尝试在我的组件中有一个isHidden属性,然后在didInsertElement钩子中循环通过联系人,然后通信目的是检查自己 - 但是我发现 contact.get('communication')。forEach 不会受到打击,因为该级别的数据没有被从

I tried having a isHidden property in my component and then in the didInsertElement hook looping through contacts and then communications with the aim of checking myself - however I find contact.get('communications').forEach doesn't get hit because that level of data hasn't been pulled from the server at that point.

推荐答案

对于第一个问题,您可以编写一个帮手来执行此操作:

For the first question, you could write a helper to do that:

// helpers/contains-john.js
export default function(array) {
    return array.any((item) => item.get('firstName') === 'John');
};

然后使用它作为一个子表达式,就像你以上一样:

Then use it as a subexpression just like you have above:

{{#if (contains-john notification.account.contacts)}}
    Stuff
{{/if}}

这篇关于在ember组件模板中,如何使用'if'来检查属性等于X的数组中的至少一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆