I have two type of pieces one is Authors and second one is Articles. I can show the list of articles (by using indexAjax.html
and index.html
in articles-pages
) as it is implemented in Apostrophe's Demo and the list of Authors as well (by using indexAjax.html
and index.html
in authors-pages/views
).
My question is how can I show the articles written by some specified author in by using show.html
file of authors-pages/views
?
So when user clicks on some author he/she should be able to see the list of articles written by the specified author.
The lib/modules/articles/index.js
file is the following
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
}
}
},
....
]
};
Any help will be appreciated!
I'm the architect of Apostrophe at P'unk Avenue.
The code in your own solution effectively reimplements a feature we already have: reverse joins.
You already have a "forward" join (
joinByOne
). So usejoinByOneReverse
to make those items available from the "other side."In the schema for authors, you can write this:
This makes a
._articles
array property available on each author.It is important to note that
idField
doesn't change. We are intentionally using the same information so that we get the same content.If you wish, you can also set
ifOnlyOne: true
on that field to avoid having it fetched for the index page and other contexts where you load more than one author.Check out the guide to schemas for more information.
I found an answer for my own question (before seeing the posted alternative answer). Hope it will be useful for others.
So there are two pieces
articles
andauthors
.lib/modules/authors-pages/index.js
lib/modules/authors-pages/view/show.html