Backbone-Associative gives simple mono- or bi-directional associations
to Backbone models using the hasOne
, hasMany
, and belongsTo
association names
familiar from ActiveRecord in Rails. It also lets models delegate attributes to associated models, while respecting that model's defaults and validation rules, and letting the delegating model listen to change:delegatedAttribute
events. While heavily influenced by Backbone-Relational, Backbone-Associative differs in two key ways:
- Associations (and reverse associations) are assigned in their own model classes, rather than assigning both in a single model class.
- Associations are declared in a chained, sentence-like API, rather than by passing in an options hash.
//Backbone-Associative
House = Backbone.AssociativeModel.extend({
associations: function() {
this.hasMany('occupants').viaReverseKey('livesIn').modelClass('Person');
}
});
Person = Backbone.AssociativeModel.extend({
associations: function() {
this.hasOne('house');
}
});
//Backbone-Relational
House = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasMany,
key: 'occupants',
relatedModel: 'Person',
includeInJSON: Backbone.Model.prototype.idAttribute,
collectionType: 'PersonCollection',
reverseRelation: {
key: 'livesIn'
}
}
]
});
Usage
To use Backbone-Associative,
-
Extend your model classes from
Backbone.AssociativeModel
. -
Add an
associations
function as an instance property on your Backbone.AssociativeModel classes. Note: associations that depend on other associations should be specified after the associations on which they depend.Article = Backbone.AssociativeModel.extend({ associations: function() { this.hasMany('comments'); } }); Comment = Backbone.AssociativeModel.extend({ associations: function() { this.belongsTo('article'); } });
-
Delegate attributes by adding a
delegateAttributes
instance property:Article = Backbone.AssociativeModel.extend({ associations: function() { this.hasMany('comments'); } }); Comment = Backbone.AssociativeModel.extend({ associations: function() { this.belongsTo('article'); }, delegateAttributes: { 'subject': 'article' } });
Associations
hasOne
andhasMany
associations indicate that another class has a reference to the class to which you assign thehas
association.belongsTo
associations are the dependent side of the association. When a model'sbelongsTo
association is destroyed, it is destroyed as well.
Options
collection
- pass a custom collection model class to be used for ahasMany
association.viaKey
means "pluck the key named X and assign it to an attribute on the model in which this association is being declared (the attribute's name is the association's name)"viaReverseKey
means "where the model in which this association is being declared is identified by one of the space-separated keys that follow"as
is an option passed afterviaReverseKey
. It means "use the associationName" to look up the class of the model in this association, but assign that model to the attribute name I'm passing in toas
. Use this option when you have more than onehasOne
association using the samereverseKey
.modelClassName
is an option passed afterviaReverseKey
. BBA will use the string passed in to this option to look up the class of the model in this association, and will then assign that model to the associationName. Use this option when you have more than onehasMany
association using the samereverseKey
.
Configuration
Backbone.AssociativeModel.namespace
accepts a string or object to be used as the root namespace when looking up reverse associations.Backbone.AssociativeModel.defaultCollection
accepts a string or descendant ofBackbone.Collection
to be used as the default collection for newhasMany
collections.
License
MIT. Use and enjoy!
Credits
Backbone.Associative is heavily influenced by Backbone-Relational and owes a debt of gratitude to Paul Uithol.