Using apostrophe-blog, I've added several custom fields in lib\modules\apostrophe-blog\index.js
. One of these fields is a select
list. However, I need to dynamically populate the list values based on other properties of the article being edited. Currently, I'm trying this, but the admin modal stops working, so this doesn't look like it will work:
module.exports = {
contextual: true,
addFields: [
{
type: 'select',
name: 'featuredIn',
label: 'Featured In:',
choices: []
},
construct: function (self, options) {
self.pageBeforeSend = (req, callback) => {
var featuredIn = self.options.addFields.find(x => x.name === 'featuredIn');
featuredIn.choices = []; //do a lookup from db - self.apos.docs.find(req, { id: ?})
return callback(null);
}
}
};
Is there another approach to doing this? Currently, I'm not sure what event would I hook into to set these values, and also where I would retrieve the article id for the article being edited - self.apos.docs.find(req, { id: ?})
. Thanks in advance for guidance.
As you know I am the lead developer of Apostrophe at P'unk Avenue.
I believe you're trying to associate one piece type with another. This is a built-in feature; you just want a join.
You don't say what type of doc is being featured, and what other type of doc it's being "featured in." Already this is confusing to write about (: So let me give you an example:
SHOP SCHEMA
addFields: [
{
name: '_products',
type: 'joinByArray',
label: 'Featured:',
withType: 'product',
idsField: 'productIds',
sortable: true
}
]
Boom... that's it. When you edit a shop, you automatically get a field allowing you to autocomplete the names of products and select them. And then when you are viewing a shop, you will find that the ._products
property is populated for you.
The idsField
property is just a mongodb property that will store the actual array of chosen product ids. You can give it any name but we recommend this naming convention.
NOTE: you must use a leading _
for a join's name
. If you don't the join winds up stored in the database, which is redundant and also almost immediately out of date. The _
means "I am a dynamic, temporary property, don't store me. I'll get calculated again every time I am needed."
So far so good. But sometimes you have a product and you want to know what shops it is featured in. No problem:
PRODUCT SCHEMA
addFields: [
{
name: '_shops',
type: 'joinByArrayReverse',
label: 'Featured In:',
withType: 'shop',
idsField: 'productIds'
}
]
This is a "reverse join." It makes a _shops
array available any time you have a product
object. It works by locating the shops that include this product in their productIds
array.
You cannot edit a join from the reverse end. If that's the end where you want to do the editing, just change which party has joinByArray
and which party has joinByArrayReverse
.
Hope this is helpful!
Also see our schema guide which covers joins in detail.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments