2017-03-09 80 views
2

我有兩種類型的零件之一是作者和第二個是文章。我可以顯示文章列表(在articles-pages中使用indexAjax.htmlindex.html),因爲它在撇號的Demo和作者列表中執行(使用中的indexAjax.htmlindex.html)。如何將碎片放入其他碎片?

我的問題是,如何使用show.html文件authors-pages/views顯示某些指定作者寫入的文章?
因此,當用戶點擊某個作者時,他/她應該能夠看到指定作者寫的文章列表。

lib/modules/articles/index.js文件如下

module.exports = { 
    extend: 'apostrophe-pieces', 
    name: 'articles', 
    label: 'Article', 
    pluralLabel: 'Articles', 
    contextual: true, 
    addFields: [ 
     { 
      name: '_author', 
      type: 'joinByOne', 
      withType: 'author', 
      label: 'Author', 
      idField: 'author', 
      filters: { 
       projection: { 
        title: 1, 
        slug: 1, 
        type: 1, 
        tags: 1 
       } 
      } 
     }, 

.... 

    ] 

}; 

任何幫助將不勝感激!

+0

嗨Karlen,使用joinByArray [指導](http://apostrophecms.org/docs/tutorials/getting-started/schema-guide.html#code-join-by-array-code)作爲起點。 –

+0

Hi @ robert-jakobson,感謝您的評論。我已經找到了解決辦法。我不想使用'joingByArray',因爲一旦任何文章創建的文章的ID應該被添加到該列表中,因此它會帶來一對一的數據庫文檔之間的鏈接,我認爲這不是一個好主意。 – Karlen

回答

3

我是在P'unk大道撇號的建築師。

您自己的解決方案中的代碼有效地重新實現了我們已有的功能:反向連接。

您已經有一個「正向」連接(joinByOne)。因此請使用joinByOneReverse使這些項目可從「其他方面」獲得。

在架構作者,你可以這樣寫:

addFields: [ 
    { 
    name: '_articles', 
    type: 'joinByOneReverse', 
    withType: 'articles', 
    label: 'Articles', 
    idField: 'author', 
    filters: { 
     projection: { 
     title: 1, 
     slug: 1, 
     type: 1, 
     tags: 1 
     } 
    } 
    } 
] 

這使得每個作者可用._articles數組屬性。

重要的是要注意,idField不會改變。我們故意使用相同的信息,以便獲得相同的內容。

如果您願意,您也可以在該字段上設置ifOnlyOne: true,以避免在索引頁和加載多個作者的其他上下文中獲取它。

查看guide to schemas瞭解更多信息。

+1

謝謝。我幾乎查過了所有文檔頁面,但沒有意識到這個選擇或忘記了,或者我忽略了文檔。無論如何,感謝偉大的平臺,併爲您的答案! – Karlen

+0

不客氣! –

1

我找到了自己的問題的答案(在看到發佈的備選答案之前)。希望它對其他人有用。

所以有兩塊articlesauthors

lib/modules目錄/作者-頁/ index.js

module.exports = { 
    extend: 'apostrophe-pieces-pages', 
    articlesPerPage: 3, 
    construct: function (self, options) { 
     var beforeShow = self.beforeShow; 
     self.beforeShow = function (req, callback) { 
      var limit = self.options.articlesPerPage; 
      var skip = req.query.page || 1; 
      skip = (parseInt(skip) - 1) * limit; 
      if (!req.data || !req.data.piece) { 
       return beforeShow(req, callback); 
      } 
      var cursor = self.apos.docs.getManager('articles').find(req, { 
       author: req.data.piece._id 
      }); 
      cursor 
       .skip(skip) 
       .limit(limit) 
       .sort({ createdAt: -1 }) 
       .toArray(function (err, articles) { 
        req.data._articles = articles || []; 
        req.data.currentPage = Math.ceil((skip + 1)/limit); 
        cursor.toCount(function(err, count){ 
         if(err) 
          count = 1; 
         req.data.totalPages = Math.ceil(count/limit); 
         beforeShow(req, callback); 
        }) 

       }) 
     } 
    } 
} 

lib/modules目錄/作者的頁面/視圖/ show.html

{% extends data.outerLayout %} 
{% block title %} 
    {{ data.piece.title }} 
{% endblock %} 
{% block main %} 
    {% for article in data._articles %} 
     <p>{{article.title}}</p>  
    {% endfor %} 
    {% import 'apostrophe-pager:macros.html' as pager with context %} 
    {{ pager.render({ page: data.currentPage, total: data.totalPages }, data.url) }} 
{% endblock %}