title: Using functions to manage relationships in Backbone
body: <p>I am working (for the first time) with Backbone.js in creating a reasonably simple application. I am currently working with two entities that have a one-to-many relationship:</p>
  
  <ul>
  <li>Project: has many groups</li>
  <li>Group: belongs to one project</li>
  </ul>
  
  <p>I looked at a few different ways of representing relationships between models, from Backbone's own recommendations to Ligament to Backbone-relational. However, I eventually settled on using simple function calls. For example, within my 'Project' model, there's a function called groups():</p>
  
  <pre class="lang-js prettyprint-override"><code>groups: function() {
    return new Collections.Groups(groups.where({project_id: this.get('id')}));
  }
  </code></pre>
  
  <p>And within the Group model:
  </p>
  
  <pre><code>project: function() {
    return projects.where({id: this.get('project_id')})[0];
  }
  </code></pre>
  
  <p>This allows me to do things like:
  </p>
  
  <pre><code>var foo = new Project();
  var foo_groups = foo.groups();
  foo_groups.at(0).get('name');
  
  var bar = new Group();
  var bar_project = bar.project();
  bar_project.get('name');
  </code></pre>
  
  <p>To me, the tradeoff here is the overhead of maintaining up-to-date relationships (when using something like backbone-relational) versus the processing time used calling the 'groups()' function each time you want to get a project's groups.</p>
  
  <p>Can anyone advise me on any pitfalls I may encounter down the road as I build out more relationships in this way, perhaps recommending a better solution (other than the extensions mentioned above)? The application will eventually have about 6 entities with a variety of one-to-many and many-to-many relationships</p>