I’m using Aldeed node-simple-schema
with autoform-select2
and collection2
I’ve created a Schema and attached it to a database called Country with 3 fields: country, state and city. I populated these with almost 30000 entries. Now I want to use simple-schema
with select2
an read from this database and display it as select to the user.
Country collection
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);
import { Tracker } from 'meteor/tracker';
Countries = new Mongo.Collection('countries');
Countries.allow({
insert: () => false,
update: () => false,
remove: () => false
});
Countries.deny({
insert: () => true,
update: () => true,
remove: () => true
});
Schemas = {};
Schemas.CountriesSchema = new SimpleSchema({
country: {
type: String,
label: 'Country',
autoform: {
label: 'Country',
type: 'text'
}
},
state: {
type: String,
label: 'State',
autoform: {
label: 'State',
type: 'text'
}
},
city: {
type: String,
label: 'City',
autoform: {
label: 'City',
type: 'text'
}
}
}, { tracker: Tracker });
Countries.attachSchema(Schemas.CountriesSchema);
...
country: {
type: String,
label: 'Country',
autoform: {
type: 'select2',
afFieldInput: {
multiple: false,
select2Options: {
placeholder: 'Choose a Country',
tags: true,
style: "width: 50%",
allowClear: true,
closeOnSelect: false
},
options: function() { // Fetch data from Country Collection and display on this field
return countries.find({ fields: { country: 1 } }).map(function(c) {
return {
label: c.title,
value: c._id
}
})
}
}
}
},
...
I don’t know if this is the best solution to achieve this. I would like to avoid using template helpers.