How to source allowedValues in autoform externally?


#1

Hello,

I’m using autoforms coupled with simple-schema, collection2. I have a bunch of drop down menus which need to show exctly the same set of options in multiple places. Is there a way to source the allowedValues content externally? I know it currently works by specifying allowedValues in the schema itself, but over the course of time, any addition / deleteion made to that will have to be changed in all places.


#2

Sure thing - allowedValues takes an array so just define your shared array somewhere, and reference that array when setting your allowedValues. For example:

  1. imports/api/widgets/widget_options.js
export const widgetOptions = [
  'Widget 1',
  'Widget 2',
  ... 
];
  1. imports/api/widgets/schema.js
...
import { widgetOptions } from './widget_options';
...
const widgetSchema = new SimpleSchema({
  ...
  widgetVariations: {
    type: String,
    label: 'Variations',
    allowedValues: widgetOptions,
    ...
  },
  ...
});
...


#4

Oh wow. Many thanks for this! Let me quickly try this out. Looks like you’ve answered all of my questions posted in the help forum on different threads :slight_smile:


#5

Quick follow up question on the import - Is this really necessary? It seems to work for me even without explicitely importing the array.


#6

It depends on how you have your code structured. If you’re using Meteor’s ES2015 module support and their recommended imports directory approach (to leverage lazy loading), then yes it’s necessary.