Using functions to manage relationships in Backbone

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:

  • Project: has many groups
  • Group: belongs to one project

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():

  • backbone-relational .set() method not updating related models
  • backbone.relational - key, keySource, keyDestination
  • 'Backbone Relational' model relation adds an 'id' with a collection as a value, preventing PUT and DELETE
  • Backbone Relational: Model with 2 collections of the same type
  • Foreign key populated with an object
  • Backbone.Collection has too many elements after save
  • groups: function() {
      return new Collections.Groups(groups.where({project_id: this.get('id')}));
    }
    

    And within the Group model:

    project: function() {
      return projects.where({id: this.get('project_id')})[0];
    }
    

    This allows me to do things like:

    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');
    

    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.

    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

  • Understanding layouts in Marionette for Backbone.js
  • Underscore templating help needed - templating collections
  • change event in backbone relational not working
  • Backbone.js relations
  • Backbone Relational validationError is not return when validation fails
  • Can a Backbone.js View have a Model and a Collection?
  • One Solution collect form web for “Using functions to manage relationships in Backbone”

    Project.groups

    I have an implementation of what I call AutoUpdatedCollection.

    I use it in the scenario you have a shared Collection which contains every Model of one class, and this Collection is used to populate sub-Collections those have to be updated when the shared Collection is updated.

    Check the implementation of the Backbone AutoUpdatedCollection.

    Following your example I suppose there is a common/shared Collection called window.groups, then I’ll implement Project Model like this:

    // code simplified and no tested
    var Project = Backbone.Model.extend({
      initialize: function(){
        this.groups =
          new App.AutoUpdatedCollection({
            name:             "Project-" + this.id + "-groups",
            sourceCollection: window.groups,
            filterField:      "project_id",
            filterValue:      this.id
          }).filteredCollection;
    
        this.groups.on( "add", this.addGroup, this );
        this.groups.on( "remove", this.removeGroup, this );
      }
    });
    

    Maybe is not as simple as this, if you have any problem let me know.

    Group.project

    I’d just initialize the Group.project attribute in the Group.initialize():

    var Group = Backbone.Model.extend({
      this.project = window.projects.where({id: this.get('project_id')})[0];
    });