简体   繁体   English

每个循环的流星js条件

[英]Meteor js condition with each loop

I am very new to meteor js and trying to build a small messaging app. 我是流星js的新手,正在尝试构建一个小型消息传递应用程序。

What I want to do is I want to check a condition within the loop with 我想做的是要检查循环中的条件

Probably, something like this. 大概是这样的。

<div class="messages-box">
{{#each messages}}

    {{#if isCurrentUserCan}}
        <p>{{msg that user can do bla bla}}</p>
    {{else}}
        <p>{{msg that user can't do}}</p>
    {{/if}}

    </div>
{{/each}}
</div>

js JS

Template.body.helpers({
'isCurrentUserCan': function(){
  if ( random message box's user ID == Meteor.userId() ){
      return 'This user can do bla bla';
 }else{
      return 'This user can't do';
 }
}
});

How can I achieve it? 我该如何实现?

You're iterating over a collection of messages. 您正在遍历消息集合。 Let's say that each message includes a userId key. 假设每个消息都包含一个userId键。 You can check to see if the userId of the message is the same as the current user and return true or false accordingly. 您可以检查消息的userId是否与当前用户相同,并相应地返回true或false。

Template.body.helpers({
  'isCurrentUserCan'() {
    return this.userId === Meteor.userId();
  }
});

Inside the {{each}} this is set to the current message object so this.key directly accesses the corresponding key. {{each}}内部, this被设置为当前message对象,因此this.key直接访问相应的密钥。

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

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