Dynamically set field choices for article

lance-p

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.

Tom Boutell

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:

  • You have "shops" that sell stuff.
  • You have "products." Each product might be featured in one "shop."
  • Both are "apostrophe-pieces" subclasses, but keep in mind the techniques below work for page types too.

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."

Accessing the shops from the product side

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically alter Field choices in Django ModelForm

From Dev

Flask / Python / WTForms validation and dynamically set SelectField choices

From Dev

Custom Flask-Admin form with some select field choices set according to another select field

From Dev

Custom Flask-Admin form with some select field choices set according to another select field

From Dev

Dynamically set choices in django model based on whether adding a new record or editing existing

From Dev

How to dynamically $set a subdocument field in mongodb?

From Dev

How to set User-based form field choices in a django ModelForm (with django-allauth)

From Dev

How to set User-based form field choices in a django ModelForm (with django-allauth)

From Dev

Symfony2 - form entity field with multiple , data not preserved after choices set

From Dev

Django Multiple Levels Choices for Field

From Dev

Django field choices not properly updating

From Dev

Create instance of model with field with choices

From Dev

How to limit choices of ForeignKey choices for Django raw_id_field

From Dev

Dynamically parse yaml field to one of a finite set of structs in Go

From Dev

How to set read only field dynamically in Blade(Laravel)

From Dev

selected checkboxes(dynamically created) values should be set to destination text field

From Dev

jQuery timepicker: how to dynamically set minTime of an input field

From Dev

How to set read only field dynamically in Blade(Laravel)

From Dev

How to Dynamically set to Field in Oracle apex 5.1 button

From Dev

Why is the hidden field in the Laravel Model not working when set dynamically?

From Dev

how do i set an id of an input field created dynamically?

From Java

How to properly use the "choices" field option in Django

From Dev

How to add choices to a field via csom

From Dev

Django model - changing options in a choices field and migrate

From Dev

Add entries to the choices of a field through the admin site?

From Dev

Lazy loading a model field's choices

From Dev

filter choices for many to many field in modelform django

From Dev

Django: Cannot resolve keyword '' into field. Choices are:

From Dev

Use field value in limit_choices_to in Django

Related Related

  1. 1

    Dynamically alter Field choices in Django ModelForm

  2. 2

    Flask / Python / WTForms validation and dynamically set SelectField choices

  3. 3

    Custom Flask-Admin form with some select field choices set according to another select field

  4. 4

    Custom Flask-Admin form with some select field choices set according to another select field

  5. 5

    Dynamically set choices in django model based on whether adding a new record or editing existing

  6. 6

    How to dynamically $set a subdocument field in mongodb?

  7. 7

    How to set User-based form field choices in a django ModelForm (with django-allauth)

  8. 8

    How to set User-based form field choices in a django ModelForm (with django-allauth)

  9. 9

    Symfony2 - form entity field with multiple , data not preserved after choices set

  10. 10

    Django Multiple Levels Choices for Field

  11. 11

    Django field choices not properly updating

  12. 12

    Create instance of model with field with choices

  13. 13

    How to limit choices of ForeignKey choices for Django raw_id_field

  14. 14

    Dynamically parse yaml field to one of a finite set of structs in Go

  15. 15

    How to set read only field dynamically in Blade(Laravel)

  16. 16

    selected checkboxes(dynamically created) values should be set to destination text field

  17. 17

    jQuery timepicker: how to dynamically set minTime of an input field

  18. 18

    How to set read only field dynamically in Blade(Laravel)

  19. 19

    How to Dynamically set to Field in Oracle apex 5.1 button

  20. 20

    Why is the hidden field in the Laravel Model not working when set dynamically?

  21. 21

    how do i set an id of an input field created dynamically?

  22. 22

    How to properly use the "choices" field option in Django

  23. 23

    How to add choices to a field via csom

  24. 24

    Django model - changing options in a choices field and migrate

  25. 25

    Add entries to the choices of a field through the admin site?

  26. 26

    Lazy loading a model field's choices

  27. 27

    filter choices for many to many field in modelform django

  28. 28

    Django: Cannot resolve keyword '' into field. Choices are:

  29. 29

    Use field value in limit_choices_to in Django

HotTag

Archive