blueren
blueren

Reputation: 2860

Is there a way to reuse a template for storing data into different identical collections using meteor SimpleSchema + Autoforms?


I'm updating the original question a bit since I've found answer to the first section of what I'd been looking for.

NEW QUESTION:

As of now, I have a single schema definition that attaches itself to 3 collection namely C1, C2, C3.

 some_schema = new SimpleSchema ({
    ...
    });
C1.attachSchema(some_schema);
C2.attachSchema(some_schema);
C3.attachSchema(some_schema);

The end goal of what I'm doing is:

If User chose option B via option A, then the form must be stored in C1 Collection. Similarly, if user chose option A via option C, then the form must be stored in C2 collection. The form that is presented to the user by itself is identical no matter how the user ended to with option A.

When it came to the templates, I had to replicate them three times as there can't exist multiple templates with identical names.

My only gripe with the above approach is that if there are 30 instead of 3 templates, then I'll have to end up dealing with maintaining 30x3 = 90 templates with different template names even though they are identical!

Would you happen to know any better means to handle this?


ORIGINAL QUESTION

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

Upvotes: 0

Views: 47

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20227

Why would these schemas be conditional? Are you using multiple schemas for each of the 3 collections? The purpose of the schema is to validate the structure of inserts and updates. Also a schema isn't really meant to be attached/removed dynamically. I would recommend just doing:

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

And then doing your insert into the appropriate schema based on your option.

Finally, if you have 3 identical schemas, why not just combine them into one and distinguish them with a type variable of some kind (essentially the value of your option variable.

doc.type = option;
collection.insert(doc);

Otherwise you're always going to have this awkward collection picker whenever you read or write one of these objects.

Upvotes: 1

Related Questions