PaulUithol/Backbone-relational — pull request #87 by DouweM (2012-03-17 02:45:33+00:00)
Title: Add support for "submodels".

* Allow models to be considered a part of their supermodel (the model type they extend from), so they share a Store collection. This means relations regarding the supermodel will also return instances of the submodel. See the added `partOfModel` section in the README.
* Allow other models than the relation's `relatedModel` to be built through a new `modelBuilder` property or through an overridden collection `#model()` method.

These two work together to allow for the following situation:

```javascript
var PetAnimal = Backbone.RelationalModel.extend({
});
var Dog = PetAnimal.extend({
partOfModel: PetAnimal
});
var Cat = PetAnimal.extend({
partOfModel: PetAnimal
});

var PetsCollection = Backbone.Collection.extend({
model: function( attrs, options) {
if (attrs.type == "cat" ) {
return new Cat( attrs, options )
}
if ( attrs.type == "dog" ) {
return new Dog( attrs, options )
}
return new PetAnimal( attrs, options )
}
});

var NewPerson = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'pets',
relatedModel: PetAnimal,
collectionType: PetsCollection,
reverseRelation: {
key: 'owner'
}
}]
});

var person = new NewPerson({
pets: [{
type: "dog",
name: "Spot"
},
{
type: "cat",
name: "Whiskers"
}]
});

ok( person.get("pets").at(0) instanceof Dog );
ok( person.get("pets").at(1) instanceof Cat );
```