/backbone-relational-model

Simplistic extension to Backbone.Model that allows models to manage has-many associations

Primary LanguageJavaScript

Backbone Relational Model

Introduction

This is a simplistic script that extends Backbone.Model to allow your models to manage has-many associations.

Usage

window.Blog = RelationalModel.extend({
  urlRoot: '/blogs',
  idAttribute: "blog_id",
  associations: {
    categories: Categories
  }
});

window.Categories = Backbone.Collection.extend({
  url: '/categories',
  model: Category
});

blog = new Blog({name: "My Story"});
catgory = new Category({id: 1});
blog.get("categories").add(category);
blog.save();  

In the associations hash you specify the collection class and you can also provide an options hash with the attributes silent (to not trigger change events on association changes) and model (for the model class to instantiate in your collection, defaults to the model class specified by the collection).

window.Blog = RelationalModel.extend({
  urlRoot: '/blogs',
  idAttribute: "blog_id",
  associations: {
    categories: [Backbone.Collection, {model: Category, silent: true}]
  }
});

If model/collection classes needed in the associations hash are not loaded yet you can wrap the declaration in a function:

window.Blog = RelationalModel.extend({
  urlRoot: '/blogs',
  idAttribute: "blog_id",
  associations: function() {
    return {
      categories: [Backbone.Collection, {model: Category, silent: true}]
    };
  }
});

When doing a create/update it's up to the backend to connect the model with its associations. Similarly, when doing a fetch on a model or an association, it's up the backend to return the models associations. If, for example when fetching a whole collection, an association is not returned by the backend then that association will be undefined and it will not be passed back to the backend on any subsequent update. This is in order to allow updating attributes of models in a collection eventhough associations are not loaded (or only partially loaded). It relies on the backend being able to distinguish between an associated collection being undefined (which means the client doesn't know about it) and it being empty (which means it should be cleared).

License

This library is released under the MIT license.

Resources