I’m building my first app, and implemented a bootstrap modal to do data entry for one of my collections. I’m not using autoform or any other packages to help with the form generation/data binding. At this point I have everything working like I want with the exception of a select/dropdown list.
I’ve got the modal working for both adding new records and updating existing records. The select list is grabbing data from a related collection, presenting the correct dropdown choices and sending the right values back to the database. The only part I don’t have working is the “edit mode”, where I retrieve a record and need the select list to show the value that comes from the database as the selected value.
It would seem to be a pretty common pattern for meteor apps but I’m not having a lot of luck finding a good forum post or blog about implementing this. Also looked through the documentation and not finding anything right away.
Here’s my HTML code in the modal template:
<div class="form-group">
<label for="category-select">Category</label>
<select id="category" >
<option disabled="disabled" selected="selected">Please Select</option>
{{#each categories}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</div>
The categories
are coming out of a helper in the template.
Template.productModal.helpers({
categories:function(){
return SubCategories.find().map(function (doc) {
return doc.name;
})
},
...
...
...
}
});
And then of course there’s an event on the template to support the save operation, which makes calls to the server-side methods. As I said, all of that works great. The only problem is when I’m in edit mode I don’t know how to tell the select list which value is coming from the database and should be shown as the selected value. Any pointers on the best way to do this?
Also, I’m curious if people generally use a package to support this kind of thing? Or are you just “rolling your own”? I’m sure if it’s a simple line of code that there’s no need for a package helper. Advice from experienced developers would be appreciated.