Polymorphism & ember.js data HowTo?


Some example code using com.fasterxml jackson


@Inheritance(strategy = InheritanceType.JOINED)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
    @JsonSubTypes.Type(value=Animal.class, name="animal"),
    @JsonSubTypes.Type(value=Dog.class, name="dog"),
    @JsonSubTypes.Type(value=Cat.class, name="cat"),
public class Animal {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private int a;


public class Dog extends Animal {
    private int d;


public class Cat extends Animal {
    private int c;


public class Animals extends ArrayList<Animal> {
private static final long serialVersionUID = 1L;

 public Animals(Collection<? extends Animal> c) {

@Produces({"application/xml", "application/json"})
public Animals findAll() {
    Animals animals = new Animals(getListAnimalsFromSomWhere());
    return animals;

Calling getAnimals will return something like


Can ember data (howto?) create a Animal, Dog & Cat object of this using type property or only animal objects?


I’m not sure if this is what you meant, but you can declare classes that use mixins. I’m pretty sure you can create a mixin and use it when declaring models.

App.FlyingMixin = Object.Mixin.create({
    fly: function() {
    height: DS.attr()

App.BarkingMixin = Object.Mixin.create({
    bark: function() {
    aggression: DS.attr()

App.AggressiveFlyingAnimal = Ember.Model.extend( [App.FlyingMixin, App.BarkingMixin], {
    type: DS.attr()

I would be surprised if this didn’t work.