3
votes

I've got some linked schemas and am trying to access properties of a subschema from the form for the primary schema. It's a mouthful, I know. Code may help:

//js
Collection = new Meteor.Collection('collection');
Schemas = {};
Schemas.secondary = new  SimpleSchema({
    unitType: {
       type: String,
      autoform: {
        type: 'select',
       options: //function with all the options 
     }
    }
});

Schemas.primary= new SimpleSchema({
    name: {
        type: String,
    },
        units: {
        type: [ Schemas.secondary ]
    }
});

Collection.attachSchema(Schemas.primary);


//HTML
{{#autoForm collection="Collection" id="someId" type="insert"}}
   {{> afQuickField name='name'}} // this works
   {{> afQuickField name='units'}} // this works
   {{> afQuickField name='units.unitType'}} // this doesn't work :-(
{{/autoForm}}

The reason I'm doing this is because there are other properties in the secondary schema that I want to show conditionally, based on the value of the select box. I also tried to put a form inside a form and then run {{#each afFieldNames name='"units"}} but that didn't quite work either. Instead of giving me just the fields contained in units (i.e., the secondary schema), it looped through all fields of both primary and secondary.

Thoughts? I'm not married to this pattern but I can't think of another way.

Thanks again, all. db

1
try {{> afQuickField name='units.1.qty'}} {{> afQuickField name='units.1.unitType'}} - sites
The answer is to create custom subtemplates for each schema option you inherit from. I'll post a fildde in a bit. - Daniel Bernhard

1 Answers

0
votes

I had this issue myself.

Give this a go

{{> afQuickField scope='units.unitType' name='units.unitType'}} 

If you dump your modifier in your before submit hook you should be able to see the subdocument successfully filled out

AutoForm.hooks({
  someId: {
    before: {
      'insert': function(modifier) {
        console.log(modifier);
      }
    }
  }
});

Let me know if this works for you!

All the best, Elliott