title: In Meteor, how to update one db field when another field changes?
body: <p>General question: in Meteor, what's the best way to implement business logic that triggers whenever a model is updated -- e.g., for updating dependent fields or validations or...</p>
  
  <p>Specific example: I'd like to add a "slug" field to Lists collection in the <a href="http://www.meteor.com/examples/todos" rel="noreferrer">Meteor todos example</a>. The slug needs to automatically update whenever a list's name is changed.</p>
  
  <p>Here's what I've got... I'm observing <em>every</em> change to a list to see if its slug needs to be created/updated. This is in a <em>shared</em> models.js (runs server and client-side, to get the benefits of latency compensation):</p>
  
  <pre class="lang-js prettyprint-override"><code>// Lists -- {name: String}
  Lists = new Meteor.Collection("lists");
  
  var listsObserver = Lists.find().observe({
      added: updateSlug,
      changed: updateSlug
  });
  
  function updateSlug(doc, idx) {
      var slug = (doc.name || '').replace(/\W+/g, '-').toLowerCase();
      if (slug !== doc.slug) {
          console.log("Updating slug for '" + doc.name + "' to " + slug);
          Lists.update(doc._id, {$set: {slug: slug}});
      }
  }
  </code></pre>
  
  <p>(And as in the original todos example, server/publish.js publishes all of <code>Lists.find()</code> as "lists", and client/todos.js subscribes to that collection.)</p>
  
  <p>The code above seems to work, but somehow doesn't look quite right to me. Questions:</p>
  
  <ol>
  <li>Is observing the Lists collection like this a reasonable approach? It seems like
  it could be inefficient -- <em>any</em> change to a Lists document will trigger this code.</li>
  <li>Should I be doing a different (simulated) update client-side, or is it OK to let
  this same Mongo/Minimongo update run on both?</li>
  <li>Do I need to call <code>listsObserver.stop()</code> at some point to dispose the observer?
  And if so, when?</li>
  </ol>
  
  <p>(I'm just getting started with Meteor, so perhaps my biases from other environments are leaking through. The implied meta-question here is, am I even thinking about this problem in the right way?)</p>