Autform - populate options from other collection


#1

Hi,

I’ve got a Collection and a SimpleSchema, and I’d like to populate a set of options from another collection, but switch between multiple collections based on the selection of another drop down. Here is my working attempt:

  linkToType: {
    label: 'Link to Kind',
    type: String,
    optional: true,
    allowedValues: ['grid', 'content'],
    autoform: {
      options () {
        return [{
          label: 'Grid',
          value: 'grid'
        }, {
          label: 'Slide Show',
          value: 'content'
        }]
      }
    }
  },
  link: {
    label: 'Link To',
    type: String,
    optional: true,
    autoform: {
      type: 'select',
      options () {
        // :TRICKY: We have to replace the slug of `this.name` which will be
        // in the form of 'panels.{index}.link'
        var type = AutoForm.getFieldValue(this.name.replace('link', 'linkToType'))
        var grids = Grids.find()
        var slideShows = SlideShows.find()
        switch (type) {
          case 'grid':
            return grids.map(grid => {
              return {label: 'Grid: ' + grid.name, value: grid.slug}
            })
          case 'content':
            return slideShows.map(slideShow => {
              return {label: 'SlideShow: ' + slideShow.name, value: slideShow.slug}
            })
        }
      }
    }
  },

So that’s actually working - the problem is, when I update a form based on that it calls that method over 1300 times (it calls it 64 times the first time - this is part of a sub-schema that is inside an iterator, the content generally has 16 instances of this).

Is there a more efficient way to grab and display that data? I assume it would be more efficient to call those find methods outside of there, and just reuse the same cursor or array, but I can’t find a good hook to put that on.

I suppose I could put that into a session var, and set this all in my router, but that’s way outside of scope, and I’d rather find an integrated way to do it.