Skip to content
Browse files

Initial commit; Backbone.RelationalModel, HasOne and HasMany.

  • Loading branch information
PaulUithol committed Apr 11, 2011
0 parents commit 37affc3d9cbf8b9ae2a1701a3a9e7782cd606a6f
Showing with 12,836 additions and 0 deletions.
  1. +73 −0
  2. +664 −0 backbone-relational.js
  3. +27 −0 test/index.html
  4. +1,094 −0 test/lib/backbone.js
  5. +8,316 −0 test/lib/jquery-1.5.1.js
  6. +219 −0 test/lib/qunit.css
  7. +1,443 −0 test/lib/qunit.js
  8. +795 −0 test/lib/underscore.js
  9. +205 −0 test/tests.js
@@ -0,0 +1,73 @@
## Backbone-relational
Backbone-relational provides one-to-one, one-to-many and many-to-one relations. To use relations, extend Backbone.RelationModel (instead of the regular Backbone.Model), and define a property 'relations', which specifies the relations. Available relation types are 'Backbone.HasOne' and 'Backbone.HasMany'.

* Bi-directional relations automatically notify related models of changes
* Decide how relations are serialized using the 'includeInJSON' option (just id, or the full set of attributes, in which case the relations of this object are in turn serialized as well)
* Convert nested objects in a model's attributes into Models when using the 'createModels' option upon initialization

### Example:

paul = new Person({
id: 'person-1',
name: 'Paul',
user: { login: 'dude', email: '' }

ourHouse = new House({
id: 'house-1',
location: 'in the middle of the street',
occupants: ['person-1']

paul.get('user').get('login'); // 'dude'

paul.get('livesIn'); // a ref to 'ourHouse', which is automatically defined because of the bi-directional HasMany relation on House to Person

ourHouse.get('occupants').remove( ); // we just made paul homeless..

paul.get('livesIn'); // see? 'null'.

This required the following relations and models:

House = Backbone.RelationalModel.extend({
// The 'relations' property, on the House's prototype. Initialized separately for each instance of House.
// Each relation must define (as a minimum) the 'type', 'key' and 'relatedModel'. Options are
// 'includeInJSON', 'createModels' and 'reverseRelation', which takes the same options as the relation itself.
relations: [
type: Backbone.HasMany, // Use the type, or the string 'HasOne' or 'HasMany'.
key: 'occupants',
relatedModel: 'Person',
includeInJSON: false,
reverseRelation: {
key: 'livesIn'

Person = Backbone.RelationalModel.extend({
relations: [
{ // Create a (recursive) one-to-one relationship
type: Backbone.HasOne,
key: 'user',
relatedModel: 'User',
reverseRelation: {
type: Backbone.HasOne,
key: 'person'

initialize: function() {
// do whatever you want :)

User = Backbone.RelationalModel.extend();

### How it works

Each Backbone.RelationalModel registers itself with Backbone.Store upon creation (and removes itself from the Store when destroyed). When creating or updating an attribute that is a key in a relation, removed related objects are notified of their removal, and new related objects are looked up in the Store.

0 comments on commit 37affc3

Please sign in to comment.
You can’t perform that action at this time.