Is there a way to conditionally attach a schema different collections?


#1

I was trying to reuse a particular schema definition that I had written. Is it possible to attach the same schema definition to different collection based on a condition?

At the end of it all, I need to store the same type of object in different collection based on what dropdown is selected.

Eg., I have a 3 options to choose from a list-group.
A
B
C

I have a generic schema defined for X.

depending on the selection made (either A B or C) I will need to attach the schema to collection1 collection2 or collection3.

Basic idea is…

some_schema = new SimpleSchema ({
...
});

if ( option A)
then collection1.attachSchema(some_schema);
else if (option B)
then collection2.attachSchema(some_schema);
else if (option C)
then collection3.attachSchema(some_schema);

Is this possible? If so, I’d really appreciate some pointers for me to head in the right direction.

Thanks


#2

I’m probably missing something, but from your example it looks like you could just always attach the same schema to each collection. So:

collection1.attachSchema(some_schema);
collection2.attachSchema(some_schema);
collection3.attachSchema(some_schema);

It sounds like which collection you use is based on the dropdown selection, so if you just attach the same schema to each collection (whether you use it with that collection or not), you should be all set.


#3

I can. But when I submit a form associated with it, wont the data be stored in all three collections since i’ve attached the same schema to all collections?


#4

No - Simple Schema just handles data validation; it doesn’t store copies of your data. Your data is always stored with the individual Collection (if using Collection2, which you are with attachSchema).


#5

Yikes. Looks like a long day of work is taking it’s toll.

A single schema can be attached to any number of collections. The collection to be persisted in is defined within the auroform declaration. Thanks!