【问题标题】:How to show pieces into the another pieces?如何将碎片展示到另一个碎片中?
【发布时间】:2023-03-15 22:40:01
【问题描述】:

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

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

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

....

    ]

};

任何帮助将不胜感激!

【问题讨论】:

  • 嗨,Karlen,使用 joinByArray guide 作为起点。
  • 嗨@robert-jakobson,感谢您的评论。我已经找到了解决方案。我不想使用joingByArray,因为一旦创建了任何文章,文章的 id 就应该添加到该列表中,因此它将在 db 文档之间带来一对一的链接,我认为这对于这种特殊情况不是一个好主意。

标签: apostrophe-cms


【解决方案1】:

我是朋克大道 Apostrophe 的建筑师。

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

您已经有一个“转发”加入 (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,以避免为索引页面和加载多个作者的其他上下文提取它。

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

【讨论】:

  • 谢谢。我几乎检查了文档的所有页面,但没有意识到这个选项或忘记了这一点,或者我可能忽略了文档。无论如何,感谢您提供的出色平台以及您的回答!
【解决方案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 %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多