emberjs/data — pull request #1845 by dvingo (2014-03-29 03:20:07+00:00)
Title: Add support for abstract polymorphic relationships

This pull request adds support for using Mixins in polymorphic relationships.

A good use case for this is when modeling tree data structures, like a filesystem for example.
An example is included in the test, but for clarity is extracted here:
```javascript
Child = Em.Mixin.create({
parent: DS.belongsTo('parent', {polymorphic: true, inverse: 'children'}),
type: DS.attr('string')
});

Parent = Em.Mixin.create({
children: DS.hasMany('child', {polymorphic: true, inverse: 'parent'}),
type: DS.attr('string')
});

Root = DS.Model.extend(Parent, {
name: DS.attr('string')
});

Folder = DS.Model.extend(Parent, Child, {
name: DS.attr('string')
});

File = DS.Model.extend(Child, {
name: DS.attr('string')
});
```
Data representation is then the same as with the current polymorphic types using `id` and `type` options.
One caveat is that an attribute `type` is needed to instantiate the concrete type when deserializing, like so:
```javascript
store.push('root', {
id: 1, name: 'root record', type: 'root',
children: [{id: 1, type: 'folder'}, {id: 1, type: 'file'}] });

store.push('folder', {
id: 1, name: 'folder 1', type: 'folder',
children: [], parent: {id: 1, type: 'root'}});

store.push('file', {
id: 1, name: 'file 1', type: 'file',
parent: {id: 1, type: 'root'}});
```
If you declare a polymorphic relationship to a Mixin and forget the `type` property, an error is thrown.
If you declare a polymorphic relationship to a DS.Model, the existing behavior is used.