Best way to use the same template to insert & update (incl dropdown list)?

I’m new to Meteor, a transplant from asp.net/sql. I have 2 questions about updating documents that contain a value that is set from a dropdown list whose values come from a Mongo collection (i.e. selecting US state or country on a user profile, etc.).

Ideally the solutions would use Blaze. I’m not quite ready for the whole React journey yet.

  1. I googled to learn how to supply the “selected” Db value of the dropdown when the update form is loading, and found solutions like this: http://stackoverflow.com/questions/12124234/set-the-selected-item-in-a-select-list-based-on-template-value All solutions I found used static values in the dropdown list, though. So my first question is "how do I first fill a dropdown list’s values from the DB (i.e. a long list of countries) and then set its “selected” value… all in one shot when the update form is loading?

  2. Most of the courses I’ve watched use separate templates for inserting vs. updating items. Whenever I built asp.net sites I always used a single page + code behind to handle inserts & updates of any given entity. If an Id was present in the query string, the 1 page was smart enough to load the existing form values and update the Db record when submitted. So my second question is “is it common to use a single template for both insert & update? If so… can anyone explain how, or provide links to the most commonly used best practices?”

Thanks

To answer you first question:

  <select id="us_state" name="us_state">
      <option value="" disabled selected>Choose a US state</option>
      {{#each states}}
      <option value="{{_id}}" {{isStateSelected _id}}>{{name}}</option>
      {{/each}}
  </select>
  <label for="us_state">US States</label>
Template.Name.helpers({
  isStateSelected: function(_id) {
    /* Your logic to check if the state is selected 
       For example: */
    if(_id === this.contact.us_state) return "selected"
  },

Some thoughts on your second question:
I personally create two forms (one for update and one for insert) but it is possible to use one.

If you are looking for a real solution in Blaze, check out https://viewmodel.org

In same form template, you could easily collect data filled with .data() and load existing data by .load()

The standard Blaze way with helpers and event handling is going to drive you crazy with the excess amount of codes you need to write and maintain.

Personally I think it’s a much cleaner solution than React + Any State Management Lib (e.g. Redux)

So simple :slight_smile: Thank you.

Thank you. I will look into this.