dhruvaray/backbone-associations — pull request #73 by dak1 (2013-10-06 22:11:54+00:00)
Title: Allow passing polymorphic models to collections

Allow polymorphic models to be used in a collection. This can be done by simply passing a function to `relatedModel` that returns the function you would normally set to a Backbone collection's `model` attribute.

```javascript
relatedModel: function (relation, attributes) {
return function (attrs, options) {
if (attrs.isBranch) {
return new Tree(attrs);
}

return new Leaf(attrs);
}
}
```

This is particularly useful when you're working with a tree with multiple models in it, as in the coffeescript example below:
```coffeescript
class Tree extends Backbone.AssociatedModel
relations: [{
type: Backbone.Many
key: 'tree'
relatedModel: (relation, attributes) ->
return (attrs, options) ->
if attrs.isBranch
return new Tree(attrs)

return new Leaf(attrs)
}]
```