2

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

我的问题是如何使用show.htmlfile of显示某些指定作者撰写的文章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
                }
            }
        },

....

    ]

};

任何帮助将不胜感激!

4

2 回答 2

3

我是朋克大道撇号的建筑师。

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

您已经有一个“前向”连接 ( joinByOne)。因此,请使用joinByOneReverse从“另一边”提供这些项目。

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在该字段上进行设置,以避免为索引页面和您加载多个作者的其他上下文获取它。

查看架构指南以获取更多信息。

于 2017-03-10T13:13:15.057 回答
2

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

所以有两件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 %}
于 2017-03-09T20:32:00.430 回答