如何将棋子显示成另一个棋子?

How to show pieces into the another pieces?

我有两种类型的文章,一种是 Authors,第二种是 Articles。我可以显示文章列表(通过在 articles-pages 中使用 indexAjax.htmlindex.html),因为它在 Apostrophe 的 Demo 和作者列表中实现(通过使用 indexAjax.htmlindex.htmlauthors-pages/views 中)。

我的问题是如何使用 authors-pages/viewsshow.html 文件显示某些指定作者撰写的文章?
所以当用户点击某个作者时he/she应该可以看到指定作者写的文章列表。

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
                }
            }
        },

....

    ]

};

任何帮助将不胜感激!

我找到了我自己的问题的答案(在看到发布的备选答案之前)。希望它对其他人有用。

所以有两块articlesauthors

lib/modules/authors-pages/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/authors-pages/view/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 %}

我是 P'unk Avenue 的 Apostrophe 的建筑师。

您自己的解决方案中的代码有效地重新实现了我们已有的功能:反向联接。

您已经有一个 "forward" 加入 (joinByOne)。所以使用 joinByOneReverse 使这些项目可以从 "other side."

authors 的架构中,您可以这样写:

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 了解更多信息。