0

我正在尝试设置一种将注册人列表添加到撇号事件模块的方法。我能够在模块中添加一个新的 joinByMany 字段,但我意识到我还需要存储一个注册日期并计算每个注册对象。为了适应这一点,我添加了以下字段,由 joinByOne 用户数组、注册日期字符串和注册计数整数组成:

addFields: [
    {
      name: '_registrants',
      label: 'Registrants',
      type: 'array',
      titleField: '_user.firstName',
      schema: [
          {
              name: '_user',
              withType: 'apostrophe-user',
              type: 'joinByOne',
              idField: 'userId',
              filters: {
                  // Fetch just enough information
                  projection: {
                      id: 1,
                      username: 1,
                      firstName: 1,
                      lastName: 1,
                      email: 1
                  }
              }
          },
          {
              name: 'registrationDate',
              label: 'Registration Date',
              contextual: true,
              type: 'string'
          },
          {
              name: 'attendeeCount',
              label: 'Total Attendees',
              type: 'integer'
          }
      ]
    }
  ]

现在,我遇到的问题是我想在我的 apostrophe-events-pages show.html 模板上显示“注册”或“取消注册”按钮,具体取决于当前登录的用户是否在_registrants 数组。之前我在使用joinByMany设置用户列表的时候,可以在JoinByMany的idsField中查找当前用户id的索引。但是,现在,我必须以某种方式遍历 _registrants 并检查每个项目 _user.userId 才能检查相同的内容,但我不知道该怎么做。我猜这将是模板中的某种逻辑,但我不确定如何在 nunjucks 中完成此操作。

当我使用 joinByMany 而不是 joinByOnes 数组时,这是我目前必须检查的用户:

          {% if data.piece.userIds.indexOf(data.user._id) < 0 %}
            <div class="event-controls-item">
              {{ buttons.major('Register for Field Trip', { action: 'register-event' }) }}
            </div>
          {% else %}
            <div class="event-controls-item">
              {{ buttons.danger('Unregister for Trip', { action: 'unregister-event' }) }}
            </div>
          {% endif %}

谢谢!

4

1 回答 1

1

您可以使用以下方法最轻松地做到这一点apos.utils.find

{% set registrant = apos.utils.find(data.piece._registrants, '_id', data.user._id) %}
{% if registrant %}
  ...
{% else %}
  ...
{% endif %}
于 2017-07-03T17:07:27.430 回答