2017-07-02 69 views
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' 
      } 
     ] 
    } 
    ] 

現在,我跑入問題是,我想根據當前登錄用戶是否位於_registrants數組中,在我的撇號事件頁面show.html模板上顯示「註冊」或「取消註冊」按鈕。以前,當我使用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 %} 

謝謝!

回答

1

你可以做到這一點最容易使用apos.utils.find

{% set registrant = apos.utils.find(data.piece._registrants, '_id', data.user._id) %} 
{% if registrant %} 
    ... 
{% else %} 
    ... 
{% endif %}